beginner · dynamic programming · tabulation · Fibonacci

How many ways up the stairs?

Problem

A staircase has n steps. Each move climbs 1 or 2 steps. In how many distinct sequences of moves can you reach the top?

Constraints

  • 1 ≤ n ≤ 30 (the answer fits in a 32-bit integer)

Examples

n = 3 → 3 — 1+1+1, 1+2, 2+1.

Show the solution

Bottom-up table

Every way to reach step i ends with a 1-step move from i − 1 or a 2-step move from i − 2, and those two groups do not overlap.

Why it is correct

By induction: ways[i − 1] and ways[i − 2] are correct, every route to i is counted in exactly one of them, so their sum is correct.

Complexity

Time O(n), extra space O(n) with the table; O(1) keeping only the last two values. Each table entry is filled once with one addition.

For contrast: Recurse on n − 1 and n − 2 without memoization. That costs Θ(φⁿ) time and O(n) stack space.

Edge cases

  • n = 1: one way.
  • ways[0] = 1 counts the empty sequence, which makes ways[2] = 2 come out right.

Common mistakes

  • Naive recursion: Θ(φⁿ) calls (see the complexity exercise on Fibonacci).
  • Overflow for large n in 32-bit integers.

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class ClimbingStairs
{
    /// <summary>Number of ways to climb n ≥ 1 steps taking 1 or 2 at a time. Bottom-up DP, O(n).</summary>
    public static int Ways(int n)
    {
        var ways = new int[n + 1];
        ways[0] = 1; // the empty sequence
        ways[1] = 1;
        for (int i = 2; i <= n; i++)
            ways[i] = ways[i - 1] + ways[i - 2]; // last move: 1 step or 2 steps
        return ways[n];
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/ClimbingStairs.cs

def ways(n: int) -> int:
    """Number of ways to climb n >= 1 steps taking 1 or 2 at a time. Bottom-up DP, O(n)."""
    table = [0] * (n + 1)
    table[0] = 1  # the empty sequence
    table[1] = 1
    for i in range(2, n + 1):
        table[i] = table[i - 1] + table[i - 2]  # last move: 1 step or 2 steps
    return table[n]

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/climbing_stairs.py

package dev.bekadan.dsa.problems;
 
public final class ClimbingStairs {
    private ClimbingStairs() {
    }
 
    /** Number of ways to climb n &gt;= 1 steps taking 1 or 2 at a time. Bottom-up DP, O(n). */
    public static int ways(int n) {
        int[] ways = new int[n + 1];
        ways[0] = 1; // the empty sequence
        ways[1] = 1;
        for (int i = 2; i <= n; i++) {
            ways[i] = ways[i - 1] + ways[i - 2]; // last move: 1 step or 2 steps
        }
        return ways[n];
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/ClimbingStairs.java

#pragma once
 
#include <cstddef>
#include <vector>
 
namespace bekadan::dsa::problems {
 
// Number of ways to climb n >= 1 steps taking 1 or 2 at a time. Bottom-up DP, O(n).
inline int climbing_stairs(int n) {
    std::vector<int> ways(static_cast<std::size_t>(n) + 1);
    ways[0] = 1;  // the empty sequence
    ways[1] = 1;
    for (std::size_t i = 2; i < ways.size(); ++i)
        ways[i] = ways[i - 1] + ways[i - 2];  // last move: 1 step or 2 steps
    return ways.back();
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/climbing_stairs.hpp