Skip to content

Commit cabffc3

Browse files
authored
Update climbing_stairs.cpp
1 parent 6243d9c commit cabffc3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cpp/neetcode_150/13_1-d_dynamic_programming/climbing_stairs.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Space: O(1)
1111
*/
1212

13-
class Solution {
13+
class SolutionBottomUp {
1414
public:
1515
//Fibonacci series : 'one' stores ways from [n-2]
1616
//'two' stores ways from [n-1]
@@ -24,3 +24,29 @@ class Solution {
2424
return one;
2525
}
2626
};
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

Comments
 (0)