
题目描述
假设你正在爬楼梯。需要 n
阶你才能到达楼顶。
每次你可以爬 1
或 2
个台阶。你有多少种不同的方法可以爬到楼顶呢?
示例 1:
输入:n = 2
输出:2
解释:有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入:n = 3
输出:3
解释:有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
提示:
解法
方法一:递推
我们定义 \(f[i]\) 表示爬到第 \(i\) 阶楼梯的方法数,那么 \(f[i]\) 可以由 \(f[i - 1]\) 和 \(f[i - 2]\) 转移而来,即:
\[
f[i] = f[i - 1] + f[i - 2]
\]
初始条件为 \(f[0] = 1\),\(f[1] = 1\),即爬到第 0 阶楼梯的方法数为 1,爬到第 1 阶楼梯的方法数也为 1。
答案即为 \(f[n]\)。
由于 \(f[i]\) 只与 \(f[i - 1]\) 和 \(f[i - 2]\) 有关,因此我们可以只用两个变量 \(a\) 和 \(b\) 来维护当前的方法数,空间复杂度降低为 \(O(1)\)。
时间复杂度 \(O(n)\),空间复杂度 \(O(1)\)。
| class Solution:
def climbStairs(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return b
|
| class Solution {
public int climbStairs(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; ++i) {
int c = a + b;
a = b;
b = c;
}
return b;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | class Solution {
public:
int climbStairs(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; ++i) {
int c = a + b;
a = b;
b = c;
}
return b;
}
};
|
| func climbStairs(n int) int {
a, b := 0, 1
for i := 0; i < n; i++ {
a, b = b, a+b
}
return b
}
|
| function climbStairs(n: number): number {
let p = 1;
let q = 1;
for (let i = 1; i < n; i++) {
[p, q] = [q, p + q];
}
return q;
}
|
| impl Solution {
pub fn climb_stairs(n: i32) -> i32 {
let (mut p, mut q) = (1, 1);
for i in 1..n {
let t = p + q;
p = q;
q = t;
}
q
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | /**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
let a = 0,
b = 1;
for (let i = 0; i < n; ++i) {
const c = a + b;
a = b;
b = c;
}
return b;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution {
/**
* @param Integer $n
* @return Integer
*/
function climbStairs($n) {
if ($n <= 2) {
return $n;
}
$dp = [0, 1, 2];
for ($i = 3; $i <= $n; $i++) {
$dp[$i] = $dp[$i - 2] + $dp[$i - 1];
}
return $dp[$n];
}
}
|
方法二:矩阵快速幂加速递推
我们设 \(Fib(n)\) 表示一个 \(1 \times 2\) 的矩阵 \(\begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}\),其中 \(F_n\) 和 \(F_{n - 1}\) 分别是第 \(n\) 个和第 \(n - 1\) 个斐波那契数。
我们希望根据 \(Fib(n-1) = \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix}\) 推出 \(Fib(n)\)。也即是说,我们需要一个矩阵 \(base\),使得 \(Fib(n - 1) \times base = Fib(n)\),即:
\[
\begin{bmatrix}
F_{n - 1} & F_{n - 2}
\end{bmatrix} \times base = \begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}
\]
由于 \(F_n = F_{n - 1} + F_{n - 2}\),所以矩阵 \(base\) 的第一列为:
\[
\begin{bmatrix}
1 \\
1
\end{bmatrix}
\]
第二列为:
\[
\begin{bmatrix}
1 \\
0
\end{bmatrix}
\]
因此有:
\[
\begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix} \times \begin{bmatrix}1 & 1 \\ 1 & 0\end{bmatrix} = \begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}
\]
我们定义初始矩阵 \(res = \begin{bmatrix} 1 & 1 \end{bmatrix}\),那么 \(F_n\) 等于 \(res\) 乘以 \(base^{n - 1}\) 的结果矩阵中第一行的第一个元素。使用矩阵快速幂求解即可。
时间复杂度 \(O(\log n)\),空间复杂度 \(O(1)\)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | import numpy as np
class Solution:
def climbStairs(self, n: int) -> int:
res = np.asmatrix([(1, 1)], np.dtype("O"))
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
n -= 1
while n:
if n & 1:
res *= factor
factor *= factor
n >>= 1
return res[0, 0]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | class Solution {
private final int[][] a = {{1, 1}, {1, 0}};
public int climbStairs(int n) {
return pow(a, n - 1)[0][0];
}
private int[][] mul(int[][] a, int[][] b) {
int m = a.length, n = b[0].length;
int[][] c = new int[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < a[0].length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
private int[][] pow(int[][] a, int n) {
int[][] res = {{1, 1}, {0, 0}};
while (n > 0) {
if ((n & 1) == 1) {
res = mul(res, a);
}
n >>= 1;
a = mul(a, a);
}
return res;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | class Solution {
public:
int climbStairs(int n) {
vector<vector<long long>> a = {{1, 1}, {1, 0}};
return pow(a, n - 1)[0][0];
}
private:
vector<vector<long long>> mul(vector<vector<long long>>& a, vector<vector<long long>>& b) {
int m = a.size(), n = b[0].size();
vector<vector<long long>> res(m, vector<long long>(n));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < a[0].size(); ++k) {
res[i][j] += a[i][k] * b[k][j];
}
}
}
return res;
}
vector<vector<long long>> pow(vector<vector<long long>>& a, int n) {
vector<vector<long long>> res = {{1, 1}, {0, 0}};
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | type matrix [2][2]int
func climbStairs(n int) int {
a := matrix{{1, 1}, {1, 0}}
return pow(a, n-1)[0][0]
}
func mul(a, b matrix) (c matrix) {
m, n := len(a), len(b[0])
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < len(a[0]); k++ {
c[i][j] += a[i][k] * b[k][j]
}
}
}
return
}
func pow(a matrix, n int) matrix {
res := matrix{{1, 1}, {0, 0}}
for n > 0 {
if n&1 == 1 {
res = mul(res, a)
}
a = mul(a, a)
n >>= 1
}
return res
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 | function climbStairs(n: number): number {
const a = [
[1, 1],
[1, 0],
];
return pow(a, n - 1)[0][0];
}
function mul(a: number[][], b: number[][]): number[][] {
const [m, n] = [a.length, b[0].length];
const c = Array(m)
.fill(0)
.map(() => Array(n).fill(0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < a[0].length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
function pow(a: number[][], n: number): number[][] {
let res = [
[1, 1],
[0, 0],
];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | /**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
const a = [
[1, 1],
[1, 0],
];
return pow(a, n - 1)[0][0];
};
function mul(a, b) {
const [m, n] = [a.length, b[0].length];
const c = Array(m)
.fill(0)
.map(() => Array(n).fill(0));
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < a[0].length; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
function pow(a, n) {
let res = [
[1, 1],
[0, 0],
];
while (n) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
|