Skip to content

Commit 0cb932e

Browse files
authored
Merge pull request #1823 from ananydev/main
Climbing Stairs Problem
2 parents 6b5eaf7 + dea088c commit 0cb932e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Climbing Stairs Problem
2+
This program provides a solution to the "Climbing Stairs" problem, a classic example of dynamic programming. The problem can be stated as follows:
3+
4+
Problem Statement:
5+
A person is at the bottom of a staircase with n steps and wants to reach the top. They can take either 1 or 2 steps at a time. The task is to determine the number of distinct ways the person can reach the top of the staircase.
6+
7+
Approach:s
8+
Dynamic Programming
9+
10+
This problem has overlapping subproblems, making it a suitable candidate for dynamic programming.
11+
Define dp[i] as the number of ways to reach the i-th step.
12+
The number of ways to reach step i is the sum of ways to reach the previous step i-1 and the step before that, i-2. This is because the person can arrive at step i by taking a single step from i-1 or a double step from i-2.
13+
Thus, the recurrence relation is:
14+
15+
𝑑𝑝[𝑖]=𝑑𝑝[𝑖−1]+𝑑𝑝[𝑖−2]dp[i]=dp[i−1]+dp[i−2]
16+
Base Cases:
17+
18+
If there are no steps (n = 0), there is 1 way (doing nothing).
19+
If there is one step (n = 1), there is also 1 way to reach it.
20+
Building the Solution:
21+
22+
Create an array dp of size n+1 to store the number of ways to reach each step up to n.
23+
Initialize dp[0] = 1 and dp[1] = 1 as per the base cases.
24+
Use a loop to fill the array from dp[2] up to dp[n] using the recurrence relation.
25+
Finally, dp[n] will contain the number of distinct ways to reach the top of the staircase with n steps.
26+
Example:
27+
For n = 5, the program calculates the ways as follows:
28+
29+
dp[0] = 1
30+
dp[1] = 1
31+
dp[2] = dp[1] + dp[0] = 2
32+
dp[3] = dp[2] + dp[1] = 3
33+
dp[4] = dp[3] + dp[2] = 5
34+
dp[5] = dp[4] + dp[3] = 8
35+
So, there are 8 distinct ways to reach the 5th step.
36+
37+
Complexity Analysis:
38+
Time Complexity: O(n) because we only need to compute each value in dp from 0 to n.
39+
Space Complexity: O(n) for storing the dp array.
40+
This dynamic programming approach efficiently computes the number of ways to climb the staircase, demonstrating how overlapping subproblems and optimal substructure can be leveraged to solve problems effectively.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
3+
int climbStairs(int n)
4+
{
5+
if (n == 0 || n == 1)
6+
{
7+
return 1;
8+
}
9+
10+
int dp[n + 1];
11+
dp[0] = dp[1] = 1;
12+
13+
for (int i = 2; i <= n; i++)
14+
{
15+
dp[i] = dp[i - 1] + dp[i - 2];
16+
}
17+
18+
return dp[n];
19+
}
20+
21+
int main()
22+
{
23+
int n = 5; // Example input
24+
printf("Ways to climb %d stairs: %d\n", n, climbStairs(n));
25+
return 0;
26+
}

0 commit comments

Comments
 (0)