↓Skip to main content
  1. LeetCode/

LeetCode 70: Climbing Stairs

·2 mins· ·
LeetCode Easy Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-04-20 Source: Day 9 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Learning Note Extract
#

LC 70 - Climbing Stairs
#

  • Status: Completed.
  • Pattern: Fibonacci-style 1D DP.

State
#

dp[i] = number of distinct ways to reach step i

Base Case
#

dp[0] = 1
dp[1] = 1

dp[0] = 1 means there is one way to start before taking any steps: do nothing.

Transition
#

dp[i] = dp[i - 1] + dp[i - 2]

To reach step i, the last move must come from step i - 1 with one step or from step i - 2 with two steps.

Complexity
#

Time: O(n)
Space: O(n) with array, O(1) with two variables

Organized Notes
#

The useful way to say this problem is not just “Fibonacci”. The state is a count of ordered step sequences. Reaching step i can only end with a 1-step from i - 1 or a 2-step from i - 2, so the two predecessor counts are disjoint and can be added. In the submitted version I use n <= 2 as the small-case shortcut, then roll the same recurrence with two variables.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

class Solution:
    def climbStairs(self, n: int) -> int:
        if n <= 2:
            return n
        prev2, prev1 = 1, 2
        for _ in range(3, n + 1):
            prev2, prev1 = prev1, prev1 + prev2
        return prev1

Complexity
#

Time O(n), Space O(1).

Mistakes To Watch
#

  • Off-by-one base cases.
  • Thinking order does not matter; sequences of 1/2 steps are distinct.

Final Interview Explanation
#

I would describe this as counting ordered step sequences. dp[i] is the number of ways to stand on step i; the last move is either one step from i - 1 or two steps from i - 2, so I add those two counts and roll the recurrence with constant space.

Related

LeetCode 338: Counting Bits
·2 mins
LeetCode Easy Dynamic-Programming Bit-Manipulation
LeetCode 338 note: dp[i] = dp[i » 1] + (i & 1), or dp[i] = dp[i - highest power of two] + 1
LeetCode 1137: N-th Tribonacci Number
·2 mins
LeetCode Easy Math Dynamic-Programming Memoization
LeetCode 1137 note: like Fibonacci, roll three variables; watch the base values T0 = 0, T1 = T2 = 1 and the loop range
LeetCode 1025: Divisor Game
·2 mins
LeetCode Easy Math Dynamic-Programming Brainteaser Game-Theory
LeetCode 1025 note: dp[i] holds when some divisor x of i makes dp[i - x] a loss; observing the pattern, the answer is simply whether n is even
LeetCode 509: Fibonacci Number
·1 min
LeetCode Easy Math Dynamic-Programming Recursion Memoization
LeetCode 509 note: the starting point of 1D DP; only the previous two terms are needed, so roll two variables
LeetCode 392: Is Subsequence
·2 mins
LeetCode Easy Two-Pointers String Dynamic-Programming
LeetCode 392 note: two pointers; the pointer on t always advances, and the one on s advances on a match; bound-check both
LeetCode 118: Pascal's Triangle
·1 min
LeetCode Easy Array Dynamic-Programming
LeetCode 118 note: each row starts and ends with 1, and every inner cell is the sum of the two cells above it