zoukankan      html  css  js  c++  java
  • [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 题目汇总

  • 相关阅读:
    蛙蛙推荐:一个程序员2012年技术学习总结
    蛙蛙推荐:第一堂编程课提纲
    蛙蛙推荐:笨办法提高代码质量
    蛙蛙推荐:Backbone和seajs搭配最佳实践探讨
    时髦的互联网公司都在用什么技术?
    蛙蛙推荐:让SecureCRT好使起来
    Linux LVM卷组管理 规格严格
    聊聊jdbc statement的fetchSize 规格严格
    老生常谈: Eclipse远程调试 规格严格
    产品经理的34个感想
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9646839.html
Copyright © 2011-2022 走看看