[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 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].
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        if (cost.length == 1) {
            return cost[0];
        }
        if (cost.length == 2) {
            return Math.min(cost[0], cost[1]);
        }
        int cur = 0, res = 0;
        int twoBefore = cost[0], oneBefore = cost[1];
        for (int i = 2; i < cost.length; i++) {
            cur = Math.min(twoBefore, oneBefore) + cost[i];
            twoBefore = oneBefore;
            oneBefore = cur;
        }
        return Math.min(twoBefore, oneBefore);
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12034501.html

时间: 2024-11-09 10:08:02

[LC] 746. Min Cost Climbing Stairs的相关文章

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: 从地面出发,走两步,走两步,走两步,走一步,走两步,走一

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

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

[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

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 所用的最小

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