EASY NC#99 Blind #16 1-D Dynamic Programming

70. Climbing Stairs

📖 Problem

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • Array DP table updates
  • State transition thinking
  • Base case initialization

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply Dynamic Programming reasoning pattern
  • Apply DFS reasoning pattern
  • Apply Recursion reasoning pattern

💡 Approach

  • Ways to reach step i = ways to reach step i-1 + ways to reach step i-2
  • Base cases: dp[0] = 1 (1 way to stay), dp[1] = 1 (1 way to take 1 step)
  • Time: O(n), Space: O(n) or O(1) with optimization
  • This is essentially Fibonacci sequence

🛠️ Hints & Pitfalls

Hints

  • Ways to reach step i = ways to reach step i-1 + ways to reach step i-2
  • Base cases: dp[0] = 1 (1 way to stay), dp[1] = 1 (1 way to take 1 step)
  • Time: O(n), Space: O(n) or O(1) with optimization

Common Pitfalls

  • This is essentially Fibonacci sequence

🧪 Test Cases

Hidden tests on submit: 4

Test Case 1
Not run
Input:
climbStairs(2);
Expected:
2
Test Case 2
Not run
Input:
climbStairs(3);
Expected:
3
Test Case 3
Not run
Input:
climbStairs(5);
Expected:
8

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit