0
0
DSA Cprogramming~10 mins

Climbing Stairs Problem in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Climbing Stairs Problem
Start at step 0
Can climb 1 or 2 steps
Calculate ways to reach step i
ways[i
Repeat until step n
Return ways[n
Start from step 0, at each step calculate ways to reach it by summing ways to previous two steps, repeat until step n.
Execution Sample
DSA C
int climbStairs(int n) {
  int ways[6] = {0};
  ways[0] = 1;
  ways[1] = 1;
  for (int i = 2; i <= n; i++) {
    ways[i] = ways[i-1] + ways[i-2];
  }
  return ways[n];
}
Calculates number of ways to climb n stairs using dynamic programming.
Execution Table
StepOperationIndex iways[i-2]ways[i-1]ways[i]Visual State of ways array
1Initialize base cases0--1[1, 0, 0, 0, 0, 0]
2Initialize base cases1--1[1, 1, 0, 0, 0, 0]
3Calculate ways[2]2ways[0]=1ways[1]=12[1, 1, 2, 0, 0, 0]
4Calculate ways[3]3ways[1]=1ways[2]=23[1, 1, 2, 3, 0, 0]
5Calculate ways[4]4ways[2]=2ways[3]=35[1, 1, 2, 3, 5, 0]
6Calculate ways[5]5ways[3]=3ways[4]=58[1, 1, 2, 3, 5, 8]
7Return resultn=5--8Final ways array: [1, 1, 2, 3, 5, 8]
💡 Reached step n=5, calculation complete, return ways[5]=8
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
i-23455
ways[0]011111
ways[1]011111
ways[2]022222
ways[3]003333
ways[4]000555
ways[5]000088
Key Moments - 3 Insights
Why do we start ways[0] and ways[1] with 1?
ways[0] = 1 means there is 1 way to stand at the bottom without climbing. ways[1] = 1 means only 1 way to reach step 1 (one step). See execution_table rows 1 and 2.
Why do we add ways[i-1] and ways[i-2] to get ways[i]?
Because to reach step i, you can come from step i-1 (one step) or step i-2 (two steps). So total ways is sum of both. See execution_table rows 3 to 6.
What happens if n is 0 or 1?
For n=0 or n=1, the base cases ways[0] and ways[1] already give the answer 1, so loop doesn't run. See variable_tracker for initial values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5, what is ways[4]?
A2
B3
C5
D8
💡 Hint
Check the 'ways[i]' column at Step 5 in execution_table.
At which step does ways[3] get its value calculated?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Index i' column and find when i=3 is processed in execution_table.
If ways[0] was initialized to 0 instead of 1, what would be the final ways[5]?
A5
B8
C7
D0
💡 Hint
Think how ways[2] and subsequent values depend on ways[0], see execution_table Step 3.
Concept Snapshot
Climbing Stairs Problem:
- ways[0] = 1, ways[1] = 1 base cases
- For i >= 2: ways[i] = ways[i-1] + ways[i-2]
- Represents ways to reach step i by 1 or 2 steps
- Return ways[n] as total ways to climb n stairs
Full Transcript
The Climbing Stairs Problem counts how many ways to reach the top of n stairs by climbing 1 or 2 steps at a time. We use an array 'ways' where ways[i] stores the number of ways to reach step i. We start with ways[0] = 1 and ways[1] = 1 as base cases. Then for each step i from 2 to n, ways[i] is calculated as the sum of ways[i-1] and ways[i-2]. This is because you can reach step i either from step i-1 by taking one step or from step i-2 by taking two steps. After filling the array up to ways[n], we return ways[n] as the total number of ways to climb n stairs. The execution table shows each step's calculation and the array state. Key moments clarify why base cases are set and how the sum works. The quiz tests understanding of array values at different steps and the effect of changing base cases.