We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6243d9c commit cabffc3Copy full SHA for cabffc3
cpp/neetcode_150/13_1-d_dynamic_programming/climbing_stairs.cpp
@@ -10,7 +10,7 @@
10
Space: O(1)
11
*/
12
13
-class Solution {
+class SolutionBottomUp {
14
public:
15
//Fibonacci series : 'one' stores ways from [n-2]
16
//'two' stores ways from [n-1]
@@ -24,3 +24,29 @@ class Solution {
24
return one;
25
}
26
};
27
+
28
29
+class SolutionTopDown {
30
+public:
31
+ int climbStairs(int n) {
32
+ if (n == 1) {
33
+ return 1;
34
+ }
35
+ if (n == 2) {
36
+ return 2;
37
38
39
+ int first = 1;
40
+ int second = 2;
41
42
+ int result = 0;
43
44
+ for (int i = 2; i < n; i++) {
45
+ result = first + second;
46
+ first = second;
47
+ second = result;
48
49
50
+ return result;
51
52
+};
0 commit comments