This problem follows the Fibonacci DP pattern, commonly found in the 1-D Dynamic Programming category. Recognizing this pattern is key to solving it efficiently in an interview setting.
dp[i] = dp[i-1] + dp[i-2]. Can optimize to two variables.
This is literally Fibonacci — the number of ways to reach step n is the sum of ways to reach n-1 (one step) and n-2 (two steps).
a, b = 1, 1
for i in range(2, n + 1):
a, b = b, a + b
return bPractice Climbing Stairs and similar 1-D Dynamic Programming problems with flashcards. Build pattern recognition through active recall.
Practice this problem