题目地址:Min Cost Climbing Stairs - LeetCode
LeetCode 动态规划(Dynamic programming)系列题目:LeetCode 动态规划(Dynamic programming)系列题目


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:

  • cost will have a length in the range [2, 1000].
  • Every cost[i] will be an integer in the range [0, 999].

这是一道经典的动态规划的题目,是爬楼梯的变种。
动态规划从前往后的Python解法如下:
此解法可以优化,不用数组,只用2个临时变量

class Solution:def minCostClimbingStairs(self, cost: List[int]) -> int:length=len(cost)if length==0 or length==1:return 0elif length==2:return min(cost[0],cost[1])count=[0 for i in range(0,length+1)]count[0]=0count[1]=0for i in  range(2,length+1):count[i]=min(count[i-2]+cost[i-2],count[i-1]+cost[i-1])return count[length]

C++解法:

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

Java解法如下:

class Solution {public int minCostClimbingStairs(int[] cost){int twoStepBefore = cost[0];int oneStepBefore = cost[1];int curr = 0;for(int i = 2;i< cost.length;i++){curr = Math.min(twoStepBefore,oneStepBefore) + cost[i];twoStepBefore = oneStepBefore;oneStepBefore = curr;}return Math.min(oneStepBefore,twoStepBefore);}
}

LeetCode 746. Min Cost Climbing Stairs--动态规划--Java,C++,Python解法相关推荐

  1. 【动态规划 斐波那切数列】LeetCode 746. Min Cost Climbing Stairs

    LeetCode 746. Min Cost Climbing Stairs 本博客转载自:http://www.cnblogs.com/grandyang/p/8343874.html 存在无代价的 ...

  2. Leetcode 746. Min Cost Climbing Stairs

    思路:动态规划. 1 class Solution { 2 //不能对cost数组进行写操作,因为JAVA中参数是引用 3 public int minCostClimbingStairs(int[] ...

  3. [dp]leetcode 746. Min Cost Climbing Stairs

    输入:一个数组cost,cost[i]表示越过第i个台阶的代价(可能是热量,也可能是过路费) 输出:走过这n个台阶,需要的最小代价 规则:一旦你为第i个台阶付出代价cost[i],那么你可以到达第i+ ...

  4. 746. Min Cost Climbing Stairs 题解

    leetcode.com/problems/min-cost-climbing-stairs/ minCost[n]: 达到台阶n需要消耗的最小成本 Cost[n]: 台阶n自身的成本 递推式为 mi ...

  5. C#LeetCode刷题之#746-使用最小花费爬楼梯( Min Cost Climbing Stairs)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4016 访问. 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个 ...

  6. LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  7. LeetCode 316. Remove Duplicate Letters--贪心--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  8. 0-1背包问题动态规划模型的Python解法

    0-1背包问题动态规划模型的Python解法 1.01背包问题 2.Python解决方案 3.01背包问题例题 1.01背包问题 背包问题(Knapsack problem)是一种组合优化的NP完全问 ...

  9. 算法:Climbing Stairs(爬楼梯) 6种解法

    说明 算法:Climbing Stairs(爬楼梯) LeetCode地址:https://leetcode.com/problems/climbing-stairs/ 题目: You are cli ...

最新文章

  1. 2021-08-29概率论—第四章随机变量的数字特征
  2. 讲解AI三大方向的模型与算法!
  3. php 扩展的so文件位置
  4. java检测tcp存活_keep-alive 和 TCP存活检测
  5. 面向数据流的设计方法
  6. debian 10安装ssh依赖openssh-client版本错误的解决办法及开启ssh远程登录设置
  7. springboot+freemarker毕业设计项目错误合集
  8. C语言编程鲍威尔算法,鲍威尔法编程-powell法编程-c语言编程-c++6.0.doc
  9. 学生每日计划表_小学生每日学习计划安排表
  10. discuz仿163k_Discuz模板-仿163k地方门户系统整站源码带数据
  11. ESP8266 AT指令设置及51单片机的控制
  12. 疫情期间,程序员是如何靠副业赚钱的?​
  13. 【技术】基于angularJS的前端自动化测试工具Protractor快速入门
  14. MAC下HFS,HFS+,HFSX文件系统 解析
  15. Kafka+Storm+HBase项目Demo(5)--topology,spout,bolt使用
  16. c语言中要求对象只能为整数的运算符是,以下正确的叙述是( ) 答案:在C程序中, %是只能用于整数运算 的运算符...
  17. USB摄像头显示为VMare USB device
  18. 三维图形在计算机如何存储,计算机三维图形技术.pdf
  19. Java程序员必看的9本基础书籍推荐!
  20. 百度628和822大K站:每个站长挥之不去的阴影

热门文章

  1. vue从后台获取新数据后刷新_vue项目中实现定时刷新页面(重新渲染数据实时更新)...
  2. Office Word 2019中找不到EndnoteX9的解决方案
  3. Docker的基本使用-Ubuntu18.04
  4. 土豆上的小霉菌引发百万人死亡和逃难,却造就全球7千万后裔
  5. pandas使用shift函数对数数据进行向上偏移(-1)或者向下偏移(1)、索引不移动,移动之后无值的赋值为NaN、将原数据列与偏移后的数据列相加生成新的数据列
  6. python使用matplotlib可视化函数曲线、设置y轴为对数坐标(log scale)、默认情况下坐标轴为线性坐标
  7. R语言使用ggplot2包geom_jitter()函数绘制分组(strip plot,一维散点图)带状图(自定义色彩、形状)实战
  8. pandas基于元组列表(list of tuples)、列表词典(dictionary of lists)、词典列表(list of dictionaries)构建dataframe数据实战
  9. 临时表,临时表什么时候删除
  10. Python裁剪图片(Crop an Image)