-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclimb-stairs.cpp
50 lines (45 loc) · 1.05 KB
/
climb-stairs.cpp
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
/**
* @file climb-stairs.cpp
* @author prakash ([email protected])
* @brief
Up to k steps in a single bound! A child is running up a staircase with n
steps and can hop between 1 and 2 steps at a time. Design an algorithm to
count how many possible ways the child can run up the stairs, as a function of
n and k. What is the running time of your algorithm?
* @version 0.1
* @date 2021-08-24
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
using namespace std;
int climb_stairs_with_n_space(int n) {
int dp[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
int climb_stairs_with_1_space(int n){
int curr,prev,prev2;
prev = 1;
prev2 = 1;
if(n<=1){
return prev;
}
for(int i=2;i<n+1;i++){
curr = prev+prev2;
prev2 = prev;
prev = curr;
}
return curr;
}
int main(int argc, const char **argv) {
int n = 5;
cout << climb_stairs_with_n_space(n) << endl;
cout << climb_stairs_with_1_space(n);
return 0;
}