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[i]代表到达这个楼梯的最小代价

  

  1. class Solution {
  2. public:
  3. int minCostClimbingStairs(vector<int>& cost) {
  4. if(cost.size()==0) return 0;
  5. if(cost.size()==1) return cost[0];
  6. vector<int> dp;
  7. int n=cost.size();
  8. dp.reserve(n+1);
  9. dp[0]=0;
  10. dp[1]=0;
  11. for(int i=2;i<=n;i++){
  12. dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
  13. }
  14. return dp[n];
  15. }
  16. };

原文地址:https://www.cnblogs.com/liangyc/p/8847703.html

时间: 2024-10-08 16:34:41

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

[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

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

[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),不太适合 动态规划 逆向解决:从后往前倒退,跳过当前阶楼梯代价最小的情况下需要考虑其面楼梯代价最小的情况,而且每次只能跳一阶或者两阶,也就是说跳

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

[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