Start learning free

LESSON 238 OF 314 · DYNAMIC PROGRAMMING

Climbing Stairs in C#: 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

    C# · DYNAMIC PROGRAMMING

    Climbing Stairs in C#, explained

    Problem overview

    ClimbStairs receives the number of steps n and returns how many distinct ways there are to climb them when each move is 1 or 2 steps. Order matters: 1 then 2 and 2 then 1 are different ways.

    For n = 4 there are five ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. The method returns 5. For n = 3 it returns 3, and for n = 2 it returns 2.

    Approach: build up from the ground

    Think about the last move. To stand on step i, you came either from step i - 1 with a 1-step move or from step i - 2 with a 2-step move. Those two groups of ways are different, and together they cover everything, so:

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

    This is the Fibonacci recurrence. Computing it recursively from the top repeats the same subproblems many times and takes exponential time. Computing it from the bottom up solves each step once. And because step i only needs the two steps below it, there is no need for a whole table: two variables are enough.

    The starting values are ways(0) = 1 (one way to stand on the ground: don't move) and ways(1) = 1 (a single 1-step move). Counting the ground as 1 makes ways(2) = 1 + 1 = 2 come out right.

    Step by step

    1. Create prev = 1. It holds the ways to reach step 0.
    2. Create curr = 1. It holds the ways to reach step 1. When n is 1 the loop never runs and curr is already the answer.
    3. Loop i from 2 while i <= n. Use <=: step n is the top and needs its own count.
    4. Compute ways = prev + curr. At the start of each round curr is the count for step i - 1 and prev the count for step i - 2.
    5. Slide the window up one step: prev = curr first, then curr = ways. Swapping the order would overwrite the value prev needs.
    6. Return curr. After the last round it holds the count for step n.

    For n = 5 the loop produces ways = 2, 3, 5, 8 for i = 2, 3, 4, 5, and the method returns 8.

    The solution in C#

    class Solution {
        public int ClimbStairs(int n) {
            int prev = 1;
            int curr = 1;
            for (int i = 2; i <= n; i++) {
                int ways = prev + curr;
                prev = curr;
                curr = ways;
            }
            return curr;
        }
    }
    

    Details worth knowing:

    • int is enough for this lesson. The answer for n = 45 is 1836311903, which fits in a 32-bit int, and the constraints stop at 45.
    • Overflow wraps silently. Beyond the constraints, C# does not warn you. The same method returns -1323752223 for n = 46 and 512559680 for n = 47. For larger n, use long (good up to n = 91) or System.Numerics.BigInteger.
    • ways lives inside the loop. A variable declared in the loop body is created fresh each round, which is what you want for a temporary value.

    Complexity

    Time: O(n). The loop runs n - 1 times with constant work in each round.

    Space: O(1). Only prev, curr, ways and i exist, however large n is. A table dp[0..n] would work too but needs O(n) memory for no benefit.

    Edge cases and common mistakes

    • n = 1. The loop is skipped and the method returns 1.
    • Starting prev at 0. It seems natural, because there are no steps on the ground. But then ways(2) comes out as 1 instead of 2.
    • Using i < n. The loop stops one round early, and the method returns the count for step n - 1.
    • Updating curr before prev. Both variables end up equal and the counts stop growing correctly.

    Testing

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