forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
43 lines (31 loc) · 731 Bytes
/
main.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
/// Source : https://leetcode.com/problems/climbing-stairs/description/
/// Author : liuyubobobo
/// Time : 2017-11-18
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
private:
vector<int> memo;
int calcWays(int n){
if(n == 0 || n == 1)
return 1;
if(memo[n] == -1)
memo[n] = calcWays(n - 1) + calcWays(n - 2);
return memo[n];
}
public:
int climbStairs(int n) {
assert(n > 0);
memo = vector<int>(n + 1, -1);
return calcWays(n);
}
};
int main() {
cout << Solution().climbStairs(10) << endl;
return 0;
}