-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclimbing_stairs.cpp
More file actions
51 lines (47 loc) · 1.57 KB
/
Copy pathclimbing_stairs.cpp
File metadata and controls
51 lines (47 loc) · 1.57 KB
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
42
43
44
45
46
47
48
49
50
51
class Solution { // O(n) time and O(n) space
public:
map<int,int> d;
int climbStairs(int n) {
if (n == 2){
return 2;
}
else if(n == 1){
return 1;
} else if (d.find(n) != d.end()){
return d[n];
}
d[n] = climbStairs(n-2) + climbStairs(n-1);
return d[n];
}
};
//Direct iterative fibonacci, without saving intermediate states! O(1) space, O(n) compute
// Binets Method (uses matrix multiplication to obtain nth fib number) [[1,1],[1,0]] ^ n = F(n) {or in our case [[1,0],[0,1]}
// But the implementation below is also good, matrix multiplication can use GPUs to be even faster!
// public class Solution {
// public int climbStairs(int n) {
// int[][] q = {{1, 1}, {1, 0}};
// int[][] res = pow(q, n);
// return res[0][0];
// }
// public int[][] pow(int[][] a, int n) {
// int[][] ret = {{1, 0}, {0, 1}};
// while (n > 0) {
// if ((n & 1) == 1) {
// ret = multiply(ret, a);
// }
// n >>= 1;
// a = multiply(a, a);
// }
// return ret;
// }
// public int[][] multiply(int[][] a, int[][] b) {
// int[][] c = new int[2][2];
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 2; j++) {
// c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
// }
// }
// return c;
// }
// }
// Another solution could have been Fibonacci formula O(log(n)) complexity, O(1) space. because pow function takes log(n)