leetcode746
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int> totol(cost.size(), );
totol[] = cost[], totol[] = cost[];
for (int i = ; i < cost.size(); i++) {
totol[i] = min(totol[i - ] + cost[i], totol[i - ] +cost[i]);
}
return min(totol[cost.size() - ], totol[cost.size() - ]);
}
};
下面是C#版本的:
public class Solution
{
public int MinCostClimbingStairs(int[] cost)
{
var total = new List<int>();
total.Add(cost[]);//第0阶台阶的最小总花费
total.Add(Math.Min(cost[], cost[] + cost[]));//第1节台阶的最小总花费 for (int i = ; i < cost.Length; i++)
{
//当前台阶的最小总花费=当前台阶的直接花费+ min(当前-1节总花费 , 当前-2节总花费)
total.Add(cost[i] + Math.Min(total[i - ], total[i - ]));
}
//最后上到楼梯顶,可能踩倒数第一节或者倒数第二节。选择其中小的
return Math.Min(total[cost.Length - ], total[cost.Length - ]);
}
}
leetcode746的更多相关文章
- [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 ...
- Leetcode746.Min Cost Climbing Stairs使用最小花费爬楼梯
数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需 ...
- leetcode746 Min Cost Climbing Stairs
""" On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 inde ...
- LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)
题目 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you p ...
随机推荐
- laravel 中禁用掉注册功能
想让 laravel 做单用户登录,禁止掉注册功能 在 RegisterController 添加以下方法 public function showRegistrationForm() { retur ...
- linux自学(五)之开始centos学习,Xshell远程连接
上一篇:linux自学(四)之开始centos学习,网络配置 前面操作都是在电脑中的虚拟机上操作的,比较麻烦,需要来回切换.下面我将使用远程连接工具Xshell进行操作,Xshell直接百度下载即可. ...
- BZOJ2131 免费的馅饼【线段树优化DP】
Input 第一行是用空格隔开的二个正整数,分别给出了舞台的宽度W(1到10^8之间)和馅饼的个数n(1到10^5). 接下来n行,每一行给出了一块馅饼的信息.由三个正整数组成,分别表示了每个馅饼落到 ...
- 推荐近乎免费的调试神器——OzCode
当一只断点打在 Visual Studio 的代码编辑器中,程序命中断点的那一刻,调试才刚刚开始……这个时候忙碌的手在键盘和鼠标之间来回跳跃,试图抓住每一次单步执行带来的状态改变. 如果命中断点的那一 ...
- window10 下 php7.0 添加Sqlserver扩展
第一步.7.0.x 7.0.x的扩展下载地址: Microsoft Drivers for PHP for SQL Server https://www.microsoft.com/en-us/do ...
- 获得消息的x,y的窗口内坐标(包括边框和titlebar高度
rectWindow = D2D.GetWindowRect(self.hwnd)#窗口大小 rectClient = D2D.GetWindowRect(self.hwnd,True)#客户区大小 ...
- cookie添加中文encoding/decoding
添加时编码,读取是解码,否则编译不过 Cookie cookie=new Cookie("姓名","黄嘎兵"); respose.addCookie(cooki ...
- Jetty小计,比之tomcat调试还是不方便
不需要额外的工程支持,不需要硬盘中有tomcat之类的额外文件 Jetty的Debug:右键Debug as... Maven Builder Jetty相关的配置文件如下: <profiles ...
- bzoj 1101 [POI2007]Zap——反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 #include<cstdio> #include<cstring& ...
- 1072. Gas Station (30) 多源最短路
A gas station has to be built at such a location that the minimum distance between the station and a ...