跳臺階問題
題目描述:
一個臺階總共有 n 級,如果一次可以跳 1 級,也可以跳 2 級。求總共有多少種跳法,并分析算法的時間復雜度。
分析和解法:
我們可以將原問題轉化為斐波那契數列問題。
解法一:經典遞歸
我們把 n 級臺階時的跳法看成是 n 的函數,記為 f(n) 。
- 當 n = 1 時,f(n) = 1;
- 當 n = 2 時,f(n) = 2;
- 當 n > 2 時,f(n) = f(n-1) + f(n-2)
原來上述問題就轉化為我們平常所熟知的 Fibonacci 數列問題了。
源代碼如下:
#include <iostream>
using namespace std;
long long Fibonacci(int n)
{
int result[3] = {0, 1, 2};
if (n <= 2)
return result[n];
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main()
{
int n;
cin >> n;
cout << Fibonacci(n) << endl;
return 0;
}
分析:時間復雜度約為 O(2 ^ n)。當然 Fibonacci 數列還有更快的實現算法。
解法二:從下往上計算
解法一之所以慢是因為重復的計算太多,只要避免重復計算就行了。比如我們可以把已經得到的數列中間項保存起來,如果下次需要計算的時候我們先查找一下,如果前面已經計算過了就不用再次計算了。更簡單的辦法是從下往上計算,首先根據 f(0) 和 f(1) 算出 f(2),在根據 f(1) 和 f(2) 算出 f(3) ……依此類推就可以算出第 n 項了。很容易理解,這種思路的時間復雜度是 O(n) 。
源代碼如下:
#include <iostream>
using namespace std;
long long Fibonacci_Solution2(unsigned n)
{
int result[3] = {0, 1, 2};
if (n <= 2)
return result[n];
long long fibNMinusOne = 2;
long long fibNMinusTwo = 1;
long long fibN = 0;
for(unsigned int i = 3; i <= n; ++ i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;
}
int main()
{
unsigned int n;
cin >> n;
cout << Fibonacci_Solution2(n) << endl;
return 0;
}
分析:時間復雜度為 O(n)。
特別注意:
下面介紹一種時間復雜度是 O(logn) 的方法計算 Fibonacci 數列。(目前還沒有修改適用于上面的問題)在介紹這種方法之前,先介紹一個數學公式:
Fibonacci 數列公式
因而計算 f(n) 就簡化為了計算矩陣的 (n-2) 次方,而計算矩陣的 (n-2) 次方,我們又可以進行分解,即計算矩陣 (n-2) / 2 次方結果的平方,逐步分解下去,由于折半計算矩陣次方,因而時間復雜度為 O(log n) 。
#include <iostream>
#include <cassert>
using namespace std;
///////////////////////////////////////////////////////////////////////
// A 2 by 2 matrix
///////////////////////////////////////////////////////////////////////
struct Matrix2By2
{
Matrix2By2
(
long long m00 = 0,
long long m01 = 0,
long long m10 = 0,
long long m11 = 0
)
:m_00(m00), m_01(m01), m_10(m10), m_11(m11)
{
}
long long m_00;
long long m_01;
long long m_10;
long long m_11;
};
///////////////////////////////////////////////////////////////////////
// Multiply two matrices
// Input: matrix1 - the first matrix
// matrix2 - the second matrix
//Output: the production of two matrices
///////////////////////////////////////////////////////////////////////
Matrix2By2 MatrixMultiply
(
const Matrix2By2& matrix1,
const Matrix2By2& matrix2
)
{
return Matrix2By2(
matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10,
matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11,
matrix1.m_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10,
matrix1.m_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11);
}
///////////////////////////////////////////////////////////////////////
// The nth power of matrix
// 1 1
// 1 0
///////////////////////////////////////////////////////////////////////
Matrix2By2 MatrixPower(unsigned int n)
{
assert(n > 0);
Matrix2By2 matrix;
if(n == 1)
{
matrix = Matrix2By2(1, 1, 1, 0);
}
else if(n % 2 == 0)
{
matrix = MatrixPower(n / 2);
matrix = MatrixMultiply(matrix, matrix);
}
else if(n % 2 == 1)
{
matrix = MatrixPower((n - 1) / 2);
matrix = MatrixMultiply(matrix, matrix);
matrix = MatrixMultiply(matrix, Matrix2By2(1, 1, 1, 0));
}
return matrix;
}
///////////////////////////////////////////////////////////////////////
// Calculate the nth item of Fibonacci Series using devide and conquer
///////////////////////////////////////////////////////////////////////
long long Fibonacci_Solution3(unsigned int n)
{
int result[2] = {0, 1};
if(n < 2)
return result[n];
Matrix2By2 PowerNMinus2 = MatrixPower(n - 1);
return PowerNMinus2.m_00;
}
int main()
{
unsigned int n;
cin >> n;
cout << Fibonacci_Solution3(n) << endl;
return 0;
}
參考資料:《編程之法》The Art of Programming By July
斐波那契數列時間復雜度分析