[LeetCode] 746. Min Cost Climbing Stairs

本文探讨了在每一级阶梯都有一定成本的情况下,寻找从底部到顶部的最小成本路径的算法。通过动态规划(DP)的方法,文章提供了多种语言的实现方案,包括Java、Python和C++,并对比了不同实现方式的优劣。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 题目汇总

转载于:https://www.cnblogs.com/lightwindy/p/9646839.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值