Start learning free

LESSON 239 OF 314 · DYNAMIC PROGRAMMING

Min Cost Climbing Stairs in Scala: Dynamic Programming

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

    SCALA · DYNAMIC PROGRAMMING

    Min Cost Climbing Stairs in Scala, explained

    Problem overview

    The method minCostClimbingStairs(cost) receives the price of each step of a staircase. Standing on step i costs cost(i) once you pay it, and from there you may climb one or two steps. You can start on step 0 or step 1, and you want the cheapest way to reach the top, which lies just past the last step. The array has between 2 and 1000 entries, each between 0 and 999.

    For cost = [10, 15, 20] the answer is 15: start on step 1, pay 15, and take two steps to reach the top. For cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] the answer is 6. Starting on step 0 and stepping only on the cheap steps (indexes 0, 2, 4, 6, 7, 9) skips every 100.

    Approach: the cheapest total for each step

    Define the total for step i as the smallest amount you can have paid after paying for step i itself. You can only arrive at step i from step i - 1 or from step i - 2, so:

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

    Steps 0 and 1 need no calculation, because you can start on either one: their totals are just their own costs. The top is one past the last step, and it can be reached from either of the last two steps without paying anything more, so the answer is the smaller of those two totals.

    The lesson stores the totals in cost itself. Step i reads only the two entries before it, which have already been turned into totals, and then writes its own total over its price. No second array is needed.

    For [10, 15, 20] the array becomes [10, 15, 30] (step 2 is 20 + min(15, 10) = 30), and the answer is min(30, 15) = 15.

    Step by step

    1. Loop over the steps from index 2 to the last one with 2 until cost.length. Indexes 0 and 1 are left alone.
    2. Add the cheaper predecessor to the current entry: cost(i) += math.min(cost(i - 1), cost(i - 2)). After this line, cost(i) holds the cheapest total for standing on step i.
    3. Finish with the smaller of the last two totals, math.min(cost(cost.length - 1), cost(cost.length - 2)), as the method's last expression.

    The solution in Scala

    class Solution {
    
        def minCostClimbingStairs(cost: Array[Int]): Int = {
            for (i <- 2 until cost.length) {
                cost(i) += math.min(cost(i - 1), cost(i - 2))
            }
            math.min(cost(cost.length - 1), cost(cost.length - 2))
        }
    }
    

    Some Scala details in this code:

    • 2 until cost.length excludes the upper bound, so the last index used is cost.length - 1. With to the loop would read past the end of the array.
    • cost(i) += ... updates an array cell. Scala treats it as cost(i) = cost(i) + ..., and arrays are mutable, so the caller's array changes.
    • Arrays are passed by reference. The method modifies the array it was given. After minCostClimbingStairs(Array(10, 15, 20)) the caller's array holds 10, 15, 30. If the original prices are still needed, pass a copy, for example cost.clone.
    • math.min takes two numbers and returns the smaller one. It is available without an import.
    • The last expression is the result. There is no return: the line with math.min is the method's value.

    Complexity

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

    Space: O(1) extra. The totals overwrite the input, so no new array is made. The price is that the input is destroyed. Keeping the prices and tracking only the last two totals in two variables would also be O(1) and would leave the input intact.

    Edge cases and common mistakes

    • Two steps. The loop doesn't run, and the answer is the cheaper of the two. [5, 5] gives 5.
    • Zero costs. [0, 0, 0, 0] gives 0.
    • Returning the last total only. Returning cost(cost.length - 1) forgets that the top can be reached from the second-to-last step. For [10, 15, 20] that would give 30 and not 15.
    • Adding a cost twice. The totals already include each step's price. Adding cost(i) again in the final line would count the last price twice.
    • A one-element array. The notes promise at least 2 steps. With one element, cost(cost.length - 2) is cost(-1) and throws an ArrayIndexOutOfBoundsException.
    • Calling it twice on the same array. The second call would add totals on top of totals, because the array no longer holds the prices. Pass a fresh array each time.
    • Overflow. The totals are at most 1000 * 999, about a million, so Int is safe.

    Testing

    • cost = [10, 15, 20] gives 15.
    • cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] gives 6.
    • cost = [5, 5] gives 5.
    • cost = [0, 0, 0, 0] gives 0.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free