{x}
blog image

Min Cost Climbing Stairs

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

 

Example 1:

Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.

Example 2:

Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.

 

Constraints:

  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999

746. Min Cost Climbing Stairs

Description

You are given an integer array cost where cost[i] is the cost of the ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can start from the step with index 0 or the step with index 1. Return the minimum cost to reach the top of the floor.

Solutions

This approach uses recursion with memoization to avoid redundant calculations. The dfs function calculates the minimum cost to reach the top from a given step.

Time Complexity: O(n) - Each step is visited at most once. Space Complexity: O(n) - Due to the recursion depth and memoization array (or cache).

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        @cache  #Python's lru_cache decorator for memoization
        def dfs(i: int) -> int:
            if i >= len(cost):
                return 0
            return cost[i] + min(dfs(i + 1), dfs(i + 2))
        return min(dfs(0), dfs(1))

Other languages would use a similar approach, utilizing a dictionary or array to store calculated results for the dfs function. For example in Java:

class Solution {
    private Integer[] f; //Memoization array
    private int[] cost;
 
    public int minCostClimbingStairs(int[] cost) {
        this.cost = cost;
        f = new Integer[cost.length]; //Initialize with null values
        return Math.min(dfs(0), dfs(1));
    }
 
    private int dfs(int i) {
        if (i >= cost.length) {
            return 0;
        }
        if (f[i] == null) { //Check if the result is already calculated
            f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2));
        }
        return f[i];
    }
}

Solution 2: Dynamic Programming

This solution uses dynamic programming to build up the solution iteratively. f[i] stores the minimum cost to reach step i.

Time Complexity: O(n) - Single pass through the array. Space Complexity: O(n) - To store the f array.

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        n = len(cost)
        f = [0] * (n + 1)
        for i in range(2, n + 1):
            f[i] = min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1])
        return f[n]

Java example:

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] f = new int[n + 1];
        for (int i = 2; i <= n; ++i) {
            f[i] = Math.min(f[i - 2] + cost[i - 2], f[i - 1] + cost[i - 1]);
        }
        return f[n];
    }
}
 

Solution 3: Dynamic Programming (Space Optimization)

This is an optimized version of the dynamic programming approach. Since only the previous two steps are needed to calculate the current step's minimum cost, we can reduce space complexity to O(1) by using two variables instead of an array.

Time Complexity: O(n) Space Complexity: O(1)

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        f = g = 0
        for i in range(2, len(cost) + 1):
            f, g = g, min(f + cost[i - 2], g + cost[i - 1])
        return g

Java example:

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int f = 0, g = 0;
        for (int i = 2; i <= cost.length; ++i) {
            int gg = Math.min(f + cost[i - 2], g + cost[i - 1]);
            f = g;
            g = gg;
        }
        return g;
    }
}

All provided code snippets (Python, Java, C++, Go, TypeScript, Rust, JavaScript) implement these three solutions, demonstrating variations in syntax but maintaining the core logic. Choose the solution that best fits your needs in terms of readability and space optimization requirements. For most cases, Solution 3 (space-optimized DP) offers the best performance.