Skip to content

Commit 250a450

Browse files
authored
Merge pull request #521 from thevijayshankersharma/0070-solution
Add a Solution for Climbing Stairs (LeetCode Problem 70)
2 parents 160297d + a8fde42 commit 250a450

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: climbing-stairs
3+
title: Climbing Stairs (LeetCode)
4+
difficulty: Easy
5+
sidebar_label: 0070-ClimbingStairs
6+
topics:
7+
- Dynamic Programming
8+
---
9+
10+
## Problem Description
11+
12+
| Problem Statement | Solution Link | LeetCode Profile |
13+
| :---------------- | :------------ | :--------------- |
14+
| [Merge Two Sorted Lists](https://leetcode.com/problems/climbing-stairs/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/climbing-stairs/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
15+
16+
## Problem Description
17+
18+
You are climbing a staircase. It takes `n` steps to reach the top.
19+
20+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
21+
22+
### Examples
23+
24+
#### Example 1:
25+
26+
- **Input:** `n = 2`
27+
- **Output:** `2`
28+
- **Explanation:** There are two ways to climb to the top:
29+
1. 1 step + 1 step
30+
2. 2 steps
31+
32+
#### Example 2:
33+
34+
- **Input:** `n = 3`
35+
- **Output:** `3`
36+
- **Explanation:** There are three ways to climb to the top:
37+
1. 1 step + 1 step + 1 step
38+
2. 1 step + 2 steps
39+
3. 2 steps + 1 step
40+
41+
### Constraints:
42+
43+
- `1 <= n <= 45`
44+
45+
### Approach
46+
47+
To find the number of distinct ways to climb to the top of the staircase, we can use dynamic programming.
48+
49+
1. Initialize an array `dp` of size `n+1` to store the number of distinct ways to reach each step.
50+
2. Set `dp[0] = 1` and `dp[1] = 1` since there's only one way to reach the first step and the second step.
51+
3. Iterate from `2` to `n`, updating `dp[i]` as the sum of `dp[i-1]` and `dp[i-2]`.
52+
4. Finally, return `dp[n]`, which represents the number of distinct ways to reach the `n`-th step.
53+
54+
### Solution Code
55+
56+
#### Python
57+
58+
```
59+
class Solution(object):
60+
def climbStairs(self, n):
61+
if n <= 2:
62+
return n
63+
dp = [0] * (n + 1)
64+
dp[1] = 1
65+
dp[2] = 2
66+
67+
for i in range(3, n + 1):
68+
dp[i] = dp[i - 1] + dp[i - 2]
69+
return dp[n]
70+
```
71+
72+
#### C++
73+
74+
```
75+
class Solution {
76+
public:
77+
int climbStairs(int n) {
78+
if (n <= 2) {
79+
return n;
80+
}
81+
vector<int> dp(n + 1);
82+
dp[1] = 1;
83+
dp[2] = 2;
84+
for (int i = 3; i <= n; ++i) {
85+
dp[i] = dp[i - 1] + dp[i - 2];
86+
}
87+
return dp[n];
88+
}
89+
};
90+
```
91+
92+
#### Java
93+
94+
```
95+
class Solution {
96+
public int climbStairs(int n) {
97+
if (n <= 2) {
98+
return n;
99+
}
100+
int[] dp = new int[n + 1];
101+
dp[1] = 1;
102+
dp[2] = 2;
103+
for (int i = 3; i <= n; ++i) {
104+
dp[i] = dp[i - 1] + dp[i - 2];
105+
}
106+
return dp[n];
107+
}
108+
}
109+
```
110+
111+
### Conclusion
112+
113+
The "Climbing Stairs" problem can be efficiently solved using dynamic programming, where the number of distinct ways to reach each step is calculated iteratively. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem.

0 commit comments

Comments
 (0)