Start learning free

LESSON 520 OF 671 · DYNAMIC PROGRAMMING

Climbing Stairs in JavaScript: Dynamic Programming

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

    JAVASCRIPT · DYNAMIC PROGRAMMING

    Climbing Stairs in JavaScript, explained

    Problem overview

    The method climbStairs receives n, the number of steps in a staircase. Each move climbs either 1 or 2 steps, and the method returns how many different sequences of moves reach step n exactly. Order matters: 1 + 2 and 2 + 1 are different ways.

    For n = 4 the answer is 5: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. The constraints keep n between 1 and 45, and the answer for 45 is 1836311903, which is still an ordinary JavaScript number with no precision problems.

    Approach: count each step from the two below it

    Listing every sequence works for n = 4 but grows quickly. Instead, ask how you can arrive at step i. The last move was either a 1-step from step i - 1 or a 2-step from step i - 2. Those two groups of ways never overlap, because they end with different moves, so:

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

    This is dynamic programming: each answer is built from answers to smaller versions of the same problem. The base cases are ways(0) = 1 (standing on the ground is one way: don't move) and ways(1) = 1 (a single 1-step). Counting the ground as 1 is what makes ways(2) = 1 + 1 = 2 come out right.

    A naive recursive function would recompute the same values again and again. Going from the bottom up avoids that, and since step i only needs the two steps below it, two variables are enough instead of a whole array.

    Step by step

    1. Create prev with let, set to 1. It holds the ways to reach step 0.
    2. Create curr the same way, also 1. It holds the ways to reach step 1. When n is 1 the loop never runs and this is already the answer.
    3. Loop i from 2 up to and including n. The condition is i <= n, because step n needs its count too. At the start of each round, curr is the count for step i - 1 and prev for step i - 2.
    4. Add the two into a const named ways. That is the count for step i.
    5. Shift the window: prev takes the old curr, then curr takes ways. The order matters, because overwriting curr first would lose the value prev needs.
    6. Return curr. After the last round it is the count for step n.

    The solution in JavaScript

    class Solution {
        climbStairs(n) {
            let prev = 1;
            let curr = 1;
            for (let i = 2; i <= n; i++) {
                const ways = prev + curr;
                prev = curr;
                curr = ways;
            }
            return curr;
        }
    }
    

    Two JavaScript details are worth noting. The counters use let because they are reassigned in the loop, while ways is a const: a new one is created in every round and never changes after that. And because const is scoped to the loop body, ways doesn't exist after the loop, which is why the method returns curr.

    Tracing n = 5

    Starting from prev = 1 and curr = 1, the loop produces these values at the end of each round:

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

    The answer for 5 steps is 8. The sequence 1, 2, 3, 5, 8, 13 is the Fibonacci sequence, shifted by one position.

    Complexity

    The loop runs n - 1 times with a constant amount of work per round, so the time is O(n). Only prev, curr and ways are stored, so the space is O(1). A table dp[0..n] would give the same time with O(n) space.

    Edge cases and common mistakes

    • Starting prev at 0. Then step 2 would give 0 + 1 = 1 instead of 2. The ground counts as one way.
    • Looping with i < n. The loop stops one step early and returns the count for step n - 1. For n = 4 it returns 3 instead of 5.
    • Updating in the wrong order. If curr = ways comes before prev = curr, both variables end up equal to ways, and every later round just doubles it: 2, 4, 8 instead of 2, 3, 5.
    • n = 1. The loop body never runs, and the method returns 1.

    Testing

    Results from the reference solution:

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