Skip to content

Commit

Permalink
climbStairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ananydev committed Nov 9, 2024
1 parent e77a98f commit b073309
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Dynamic Programming/Climbing stairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ This program provides a solution to the "Climbing Stairs" problem, a classic exa
Problem Statement:
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.

Approach:
Dynamic Programming:
Approach:s
Dynamic Programming

This problem has overlapping subproblems, making it a suitable candidate for dynamic programming.
Define dp[i] as the number of ways to reach the i-th step.
Expand Down
38 changes: 23 additions & 15 deletions Dynamic Programming/Climbing stairs/program.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
class Solution {
public int climbStairs(int n)
#include <stdio.h>

int climbStairs(int n)
{
if (n == 0 || n == 1)
{
if (n == 0 || n == 1)
{
return 1;
}
return 1;
}

int dp[n + 1];
dp[0] = dp[1] = 1;

int[] dp = new int[n+1];
dp[0] = dp[1] = 1;

for (int i = 2; i <= n; i++)
{
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
}

return dp[n];
}

int main()
{
int n = 5; // Example input
printf("Ways to climb %d stairs: %d\n", n, climbStairs(n));
return 0;
}

0 comments on commit b073309

Please sign in to comment.