Start learning free

LESSON 239 OF 314 · DYNAMIC PROGRAMMING

Min Cost Climbing Stairs in C#: Dynamic Programming in O(n)

Practice O(n) Read the full explanation

You get an array cost for a staircase. You pay cost[i] when you stand on step i.

Return the smallest total you can pay to reach the top.

You start on step 0 or step 1, as you like. After you pay for a step, you climb 1 or 2 steps. The top is one step above the last step.

                        ______ top
                  __6__|
            __3__|
      __9__|
__4__|

Example 1:

Input: cost = [4, 9, 3, 6]
Output: 7
Explanation: This is the staircase in the picture.
Start on step 0 and pay 4.
Climb 2 to step 2 and pay 3.
Climb 2 more to reach the top.
In total, 4 + 3 = 7.

Example 2:

Input: cost = [5, 0]
Output: 0
Explanation: Start on step 1 and pay 0.
Climb 1 to reach the top.
The 5 on step 0 is never paid.

Example 3:

Input: cost = [2, 7, 1, 1, 8, 3, 2]
Output: 7
Explanation: Stand on steps 0, 2, 3 and 5.
That costs 2 + 1 + 1 + 3 = 7.
From step 5, climbing 2 reaches the top.

Constraints:

  • cost has 2 to 1,000 steps.
  • Each cost is from 0 to 999.

STEP 1 OF 3
    Ctrl + Enter to check

    C# · DYNAMIC PROGRAMMING

    Min Cost Climbing Stairs in C#, explained

    Problem overview

    A staircase has steps numbered from 0, and cost[i] is the price of stepping on step i. After paying, you may climb one or two steps. You can start on step 0 or step 1, and the goal is the top, which is one position past the last step. MinCostClimbingStairs(int[] cost) returns the cheapest total price to get there.

    For cost = [10, 15, 20] the answer is 15: start on step 1, pay 15, and jump two steps to the top. For cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] the answer is 6, by stepping only on the cheap steps. The array has between 2 and 1000 entries, each between 0 and 999.

    Approach: cheapest way to stand on each step

    Define the total for a step as the cheapest sum you can pay to stand on it, counting the step's own price. You arrive at step i only from step i - 1 or step i - 2, so

    total[i] = cost[i] + min(total[i - 1], total[i - 2])
    

    Steps 0 and 1 are starting points, so their totals are just their own prices. The top can be reached from the last step or the one before it, which makes the answer the smaller of the last two totals.

    Trying every route directly would branch twice at each step, which is exponential. The table of totals makes each step cost constant time.

    Step by step

    1. Loop from index 2 up to the end of the array. In C#, the size of an array is its Length property, without parentheses.
    2. Add the cheaper predecessor. Inside the loop, add Math.Min(cost[i - 1], cost[i - 2]) to cost[i]. The lesson reuses the input array itself as the table, so after the loop each entry holds the cheapest total for that step instead of its own price.
    3. Return the smaller of the last two totals, cost[cost.Length - 1] and cost[cost.Length - 2]. Both indexes exist because there are at least two steps.

    The solution in C#

    class Solution {
        public int MinCostClimbingStairs(int[] cost) {
            for (int i = 2; i < cost.Length; i++) {
                cost[i] += Math.Min(cost[i - 1], cost[i - 2]);
            }
            return Math.Min(cost[cost.Length - 1], cost[cost.Length - 2]);
        }
    }
    

    Here is the first example traced. Only index 2 changes: cost[2] = 20 + Math.Min(15, 10) = 30. The final line compares cost[2] = 30 with cost[1] = 15 and returns 15.

    C# details:

    • Arrays are reference types. The method receives a reference to the caller's array, so cost[i] += ... changes the caller's data. That is fine for this lesson, but if you need the original values afterwards, copy the array first or use a separate table.
    • += with Math.Min. The compound assignment reads cost[i], adds the minimum of the two previous totals, and stores the result back in the same element.
    • No overflow worries. At most 1000 steps costing at most 999 each give a total below 1,000,000, far inside the int range.

    Complexity

    Time: O(n). One pass over the array, with constant work per element.

    Space: O(1) extra. The array is reused as the table. If you copy it first to protect the input, the space becomes O(n).

    Edge cases and common mistakes

    • Two steps. The loop never runs. For [5, 3] the answer is 3: start on step 1 and step to the top.
    • Returning the last total only. The top can be reached from step n - 2 too, with a two-step jump, so the last entry alone is wrong. For [10, 15, 20] it gives 30 instead of 15.
    • Starting the loop at 0 or 1. The indexes i - 2 would be negative and throw IndexOutOfRangeException.
    • Looking for a price on the top. Only steps in the array have a cost. The top is just where you finish, so nothing is added for it.
    • Reusing an array you still need. Calling the method twice on the same array gives wrong results the second time, because the first call already overwrote the values.
    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