[LeetCode] 746. Min Cost Climbing Stairs

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3]. 

Note:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].

解法:DP,一种是设dp长度为n+1, 公式:dp[i] = min(dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1]), 最后返回dp的最后一个值。

另一种是设dp长度为n, 公式:dp[i] = cost[i] + min(dp[i- 1], dp[i - 2]), 最后返回倒数的两个数组值中小的一个。

Java: dp + greedy

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]);
    }

Java:

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

Python:

class Solution(object):
    def minCostClimbingStairs(self, cost):
        """
        :type cost: List[int]
        :rtype: int
        """
        dp = [0] * 3
        for i in reversed(xrange(len(cost))):
            dp[i%3] = cost[i] + min(dp[(i+1)%3], dp[(i+2)%3])
        return min(dp[0], dp[1])

Python:

def minCostClimbingStairs(self, cost):
    n = len(cost)
    if n == 0 or n == 1:
        return 0
    min_cost0, min_cost1 = cost[0], cost[1]
    for i in range(2, n):
        min_cost0, min_cost1 = min_cost1, min(min_cost0, min_cost1) + cost[i]

    return min(min_cost0, min_cost1)  

C++:

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();
        vector<int> dp(n + 1, 0);
        for (int i = 2; i < n + 1; ++i) {
            dp[i] = min(dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);
        }
        return dp.back();
    }
};

C++:

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();
        vector<int> dp(n, 0);
        dp[0] = cost[0]; dp[1] = cost[1];
        for (int i = 2; i < n; ++i) {
            dp[i] = cost[i] + min(dp[i- 1], dp[i - 2]);
        }
        return min(dp[n - 1], dp[n - 2]);
    }
};

  

类似题目:

[LeetCode] 70. Climbing Stairs 爬楼梯

[LeetCode] 53. Maximum Subarray 最大子数组

[Airbnb] Max Sum of Non-consecutive Array Elements

  

 

All LeetCode Questions List 题目汇总

posted @ 2018-09-14 15:23  轻风舞动  阅读(206)  评论(0编辑  收藏  举报