Start learning free

LESSON 238 OF 314 · DYNAMIC PROGRAMMING

Climbing Stairs in Go: Dynamic Programming with Two Variables

Practice O(n) Read the full explanation

You get an integer n, the number of steps in a staircase. Each move takes you up 1 step or 2 steps.

Return how many different ways you can climb from the bottom to exactly the top.

The order of the moves matters. For example, 1 + 2 and 2 + 1 are two different ways.

Example 1:

Input: n = 4
Output: 5
Explanation: The five ways are:
1. 1 + 1 + 1 + 1
2. 1 + 1 + 2
3. 1 + 2 + 1
4. 2 + 1 + 1
5. 2 + 2

Example 2:

Input: n = 1
Output: 1
Explanation: One move of 1 step is the only way.

Example 3:

Input: n = 6
Output: 13
Explanation: Group the ways by how many 2s they use:
no 2s: 1 way (1 + 1 + 1 + 1 + 1 + 1),
one 2: 5 ways (2 + 1 + 1 + 1 + 1 in any order),
two 2s: 6 ways (2 + 2 + 1 + 1 in any order),
three 2s: 1 way (2 + 2 + 2).
In total, 1 + 5 + 6 + 1 = 13.

Constraints:

  • n is from 1 to 45.
  • The answer fits in a 32-bit signed integer.
  • For n = 45, the answer is 1,836,311,903.

STEP 1 OF 6
    Ctrl + Enter to check

    GO · DYNAMIC PROGRAMMING

    Climbing Stairs in Go, explained

    Problem overview

    The function climbStairs receives the number of steps n and returns how many distinct ways there are to reach the top, climbing 1 or 2 steps at a time. Order matters: 1 step then 2 steps is a different way from 2 steps then 1. The constraints are 1 <= n <= 45, and the answer always fits in a 32-bit integer.

    For n = 4 there are five ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. For n = 3 there are three, and for n = 2 there are two.

    Approach: build the answer from the two steps below

    Think about the last move onto step i. It was either a 1-step move from step i - 1 or a 2-step move from step i - 2. Those two groups of ways never overlap, because their last moves differ, so the counts add up:

    ways(i) = ways(i - 1) + ways(i - 2)

    This is dynamic programming: each answer reuses answers for smaller inputs. Writing it as a plain recursive function would recompute the same counts again and again, and the number of calls grows exponentially. Counting from the bottom up computes each step once.

    Since ways(i) needs only the two previous counts, there is no need to store a whole table. Two variables are enough, and they move up the staircase one step per round.

    The base cases are what start the chain. Standing on the ground, step 0, counts as one way: don't move. Step 1 has one way, a single 1-step move. Counting the ground as 1 is what makes step 2 come out right, because 1 + 1 = 2.

    Step by step

    1. Create prev as 1. It holds the ways to stand on step 0.
    2. Create curr as 1. It holds the ways to reach step 1. If n is 1, the loop never runs and curr is already the answer.
    3. Loop from step 2 up to and including n. At the start of each round, curr is the count for step i - 1 and prev the count for step i - 2. The condition is i <= n, because step n needs its own count.
    4. Add the two counts into a new variable ways: the number of ways to reach step i.
    5. Shift both counts up one step. Set prev to curr first, then curr to ways. The order matters, because changing curr first would overwrite the value prev needs.
    6. Return curr. After the last round it holds the count for step n.

    Here is the loop for n = 5:

    i 2 prev 1 curr 1 ways 2
    i 3 prev 1 curr 2 ways 3
    i 4 prev 2 curr 3 ways 5
    i 5 prev 3 curr 5 ways 8

    Each ways becomes the next curr, and each curr becomes the next prev. The sequence 1, 1, 2, 3, 5, 8 is the Fibonacci sequence, so the answer for n steps is the Fibonacci number that follows n.

    The solution in Go

    func climbStairs(n int) int {
        prev := 1
        curr := 1
        for i := 2; i <= n; i++ {
            ways := prev + curr
            prev = curr
            curr = ways
        }
        return curr
    }
    

    The Go details worth a closer look:

    • := declares, = assigns. prev, curr and ways are created with :=. Inside the loop, prev = curr and curr = ways only change existing variables. Writing := there would be a compile error, because there would be no new variable on the left.
    • ways lives inside the loop body. It is created fresh in every round and disappears at the closing brace, which is why return curr is the only value that survives.
    • int is 64 bits on a 64-bit machine. The largest answer here, for n = 45, is 1836311903, which also fits in 32 bits, as the constraints promise.
    • Go has a single loop keyword. The three-part for covers what other languages write as for and while.

    Complexity

    Time: O(n). The loop runs n - 1 times and each round does a fixed amount of work.

    Space: O(1). Three integer variables, whatever the value of n.

    Edge cases and common mistakes

    • Using i < n. The loop stops one round early and returns the count for step n - 1. For n = 4 that gives 3 instead of 5.
    • Updating in the wrong order. If curr = ways comes before prev = curr, both variables end up equal to ways, and the counts double each round. For n = 4 this gives 8 instead of 5.
    • Starting prev at 0. It is tempting to say the ground has no ways to reach. Then step 2 would be 0 + 1 = 1, but there are two ways to climb two steps.
    • The smallest input. For n = 1 the loop body never runs and the function returns 1, which is correct.

    Testing

    • n = 1 gives 1.
    • n = 2 gives 2.
    • n = 3 gives 3.
    • n = 4 gives 5.
    • n = 6 gives 13.
    • n = 10 gives 89.
    • n = 45 gives 1836311903.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free