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的更多相关文章

  1. [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 ...

  2. Leetcode746.Min Cost Climbing Stairs使用最小花费爬楼梯

    数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需 ...

  3. leetcode746 Min Cost Climbing Stairs

    """ On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 inde ...

  4. 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 ...

随机推荐

  1. 内联汇编实现 memcpy 和 memset

    #pragma check_stack( off) LPVOID __cdecl _memcpy(void * dst, void* src, size_t size) { int dwSize = ...

  2. SQL Server 为存储过程添加预定设置注释代码

    一个优秀的项目最少不了的是代码注释,兴许你是代码高手入目既知道该段代码主要功能是什么,但日子长了,记的东西多了,即时再熟悉的代码也渐渐的有点不认 识它,所以养成良好的写注释的习惯,对于自己对于他人都是 ...

  3. 异常处理过程和异常处理的执行顺序(针对try{}catch{}finally{}而言)

    1.异常的处理方式有两种分别为:try{}catch{}finally{}和throws下面简单说一下这两者的区别和联系. 2.出现异常之后如果没有进行捕获处理系统就会直接将这个异常栈的跟踪信息直接打 ...

  4. rem自适应原理

    rem自适应原理 rem是根据html的font-size大小来变化,正是基于这个出发,我们可以在每一个设备下根据设备的宽度设置对应的html字号,从而实现了自适应布局.更多介绍请看这篇文章:rem是 ...

  5. altium常用快捷键记录

    选中一个网络的点和线ctrl+h: 翻转器件的层 鼠标拖动+L: 镜像器件 鼠标拖动+x: 查看单一层shift+s: 隐藏/查看某些器件ctrl+d:

  6. 【DUBBO】Dubbo:monitor的配置

    [一]:配置项 <dubbo:monitor protocol="registry"/> [二]:配置解析器-->具体解析器为com.alibaba.dubbo. ...

  7. 转发 Java火焰图在Netflix的实践

    为了分析不同软件或软件的不同版本使用CPU的情况,相关设计人员通常需要进行函数的堆栈性能分析.相比于定期采样获得数据的方式,利用定时中断来收集程序运行时的PC寄存器值.函数地址以及整个堆栈轨迹更加高效 ...

  8. jQuery的deferred对象详解(转载)

    jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本.(由于无法转载,复制原文 .原文链接——原作者:阮一峰) 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5. ...

  9. UNIX简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/h ...

  10. Python——str常用操作方法

    1. 索引(即下标) s = 'ABCDEFGHIJKLMN' s1 = s[0] print('s[0] = ' + s1) #s[0] = A print('s[3] = '+ s[3]) #s[ ...