Min Cost Climbing Stairs [746]

Min Cost Climbing Stairs [746]

题目描述

简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以最小的代价达到楼层,也就是跨过所有楼梯

问题解决

穷举法

从第一阶楼梯开始,遍历所有可能的情况,然后选择代价最小的。复杂度会比较高,时间复杂度O(2^n),不太适合

动态规划

逆向解决:从后往前倒退,跳过当前阶楼梯代价最小的情况下需要考虑其面楼梯代价最小的情况,而且每次只能跳一阶或者两阶,也就是说跳过当前fn阶楼梯,需要的代价可以表示为f(n) = cost[n]+ min(f(n+1), f(n+2))(果然编程到最后都是数学问题啊)

这样就找到了解决问题的办法。从最后的楼梯开始,f(n)=cost[n]+min(f(n+1)=0, f(n+2)=0);

f(n-1) = cost[n-1] + min(f(n), f(n+1)),以此类推

代码

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

原文地址:https://www.cnblogs.com/chinazhonghao/p/10261942.html

时间: 2024-10-10 09:22:03

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

[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

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

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

[Swift]LeetCode746. 使用最小花费爬楼梯 | 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

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