-
Notifications
You must be signed in to change notification settings - Fork 31
/
0070-climbing-stairs.cpp
66 lines (53 loc) · 2.11 KB
/
0070-climbing-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Problem: LeetCode 70 - Climbing Stairs
Description:
You are climbing a staircase that has n steps. You can either climb 1 or 2 steps at a time.
Return the number of distinct ways to climb to the top.
Intuition:
To reach the nth step, we can either take a single step from the (n-1)th step or take two steps from the (n-2)th step.
This forms the basis of our dynamic programming approach, as we can break down the problem into subproblems and build the solution from there.
Approach:
1. Initialize an array dp of size (n+1) to store the number of distinct ways to reach each step.
2. Set the base cases: dp[0] = 1 (no steps needed) and dp[1] = 1 (one step to reach the first step).
3. Iterate from 2 to n:
- Compute dp[i] by summing up the number of ways to reach the previous two steps: dp[i] = dp[i-1] + dp[i-2].
4. Return dp[n], which represents the number of distinct ways to reach the top step.
Time Complexity:
The time complexity is O(n) since we iterate through the steps once.
Space Complexity:
The space complexity is O(n) since we use an array of size (n+1) to store the intermediate results.
Dynamic Programming:
- Subproblem: The subproblem is finding the number of distinct ways to reach the current step.
- Recurrence Relation: dp[i] = dp[i-1] + dp[i-2], where dp[i] represents the number of distinct ways to reach the ith step.
- Base Case: dp[0] = 1 and dp[1] = 1, as there is only one way to reach the first two steps.
*/
class Solution {
public:
int climbStairs(int n) {
if (n <= 2) {
return n; // Base cases
}
vector<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]; // Recurrence relation
}
return dp[n];
}
};
// O(1) Space
// class Solution {
// public:
// int climbStairs(int n) {
// if(n <= 2)
// return n;
// int first = 1, second = 2;
// for(int i = 2; i < n; i++) {
// int temp = second;
// second = first + second;
// first = temp;
// }
// return second;
// }
// };