Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

题目翻译

有一个楼梯,第i阶用cost[i](非负)表示成本。现在你需要支付这些成本,可以一次走两阶也可以走一阶。 问从地面或者第一阶出发,怎么走成本最小。

测试样例

Input: cost = [10, 15, 20]
Output: 15
Explanation: 从第一阶出发,一次走两步

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: 从地面出发,走两步,走两步,走两步,走一步,走两步,走一步。

详细分析

现在用step[i]表示走到第i阶的成本,要求step[i],我们只需在"到前一阶的成本+当前阶成本"和"到前两阶的成本+前两阶成本"取最小即可。一图胜千言:

 

因为step[0]和step[1]都可以作为开始出发地,所以成本都为0。注意一下爬两阶只需要那两阶的第一个成本作为总成本不需要两阶成本相加。所以

step[2] = min{step[1]+cost[1],step[0]+cost[0]} = min{10,15}=10

step[3] = min{step[2]+cost[2],step[1]+cost[1]} = min{30,15} = 15

代码实现

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

原文地址:https://www.cnblogs.com/racaljk/p/8418830.html

时间: 2024-10-24 13:11:02

Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)的相关文章

LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)

题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步,这里用 dynamic, 从index 2 (第三格楼梯开始) 计算每一个楼梯,到达需要用的最小cost. 在每一个楼梯,只需要计算 从前面2格楼梯过来的cost, 和 从前面1格楼梯过来的 cost,哪个小.就选哪个叠加自己的cost.最后 index = len 的地方就是走到top 所用的最小

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

[LC] 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 w

[LeetCode&amp;Python] Problem 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 w

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 w

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 w

Min Cost Climbing Stairs [746]

Min Cost Climbing Stairs [746] 题目描述 简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以最小的代价达到楼层,也就是跨过所有楼梯 问题解决 穷举法 从第一阶楼梯开始,遍历所有可能的情况,然后选择代价最小的.复杂度会比较高,时间复杂度O(2^n),不太适合 动态规划 逆向解决:从后往前倒退,跳过当前阶楼梯代价最小的情况下需要考虑其面楼梯代价最小的情况,而且每次只能跳一阶或者两阶,也就是说跳

[LC难题必须要解决系列][之][DP] Min Cost Climbing Stairs

Min Cost Climbing Stairs https://leetcode.com/problems/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 min

746. Min Cost Climbing [email&#160;protected]

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 w