746. Min Cost Climbing Stairs
两种方法,核心思想都一样,求出走到每一步上的最小开销,直到最后一步和倒数第二步,比较其最小值返回即可。
方法一,用一个辅助的容器
class Solution
{
public:
int minCostClimbingStairs(vector<int>& cost)
{
int n=cost.size();
vector<int> help(n);
help[]=cost[];
help[]=cost[];
for(int i=;i<n;i++)
help[i]=cost[i]+min(help[i-],help[i-]);
return min(help[n-],help[n-]);
}
};
方法二,不用辅助容器,用两个辅助变量
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int minCostClimbingStairs(vector<int>& cost)
{
int sz=cost.size();
int left1=cost[];
int left2=cost[];
int cur=;
for(int i=;i<sz;i++)
{
cur=min(left1,left2)+cost[i];
left1=left2;
left2=cur;
}
return min(left1,left2);
}
};
746. Min Cost Climbing Stairs的更多相关文章
- LN : leetcode 746 Min Cost Climbing Stairs
lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- 746. Min Cost Climbing Stairs@python
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] 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 ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- [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 ...
- 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯
[抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...
- 【easy】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 t ...
- [LeetCode&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 ...
随机推荐
- WAS 忘记密码
一.重置密码 1.首先关闭was,ps –ef|grep java 查看java进程号,然后kill -9 XXXX杀掉进程即可.或者使用命令./stopServer.sh server1 2.取消控 ...
- 排列组合或容斥原理 SPOJ - AMR11H
题目链接: https://vjudge.net/contest/237052#problem/H 这里给你一串数字,让你计算同时拥有这串数字最大值和最小值的子集(连续)和子序列(可以不连续)的数量, ...
- PHP文件上传与下载
一:上传文件与报错 $_FILES 超全局数组,包含了有关上传文件的所有信息! 而且,这个数组中只包含文件相关信息,其他数据依然在$_POST里面$_FILES是一个二维数组,每上传一个文件,都是数组 ...
- Mac 查看端口占用及杀死进程
lsof -i : kill -
- RocketMq顺序消费
部分内容出处 https://www.jianshu.com/p/453c6e7ff81c rocketmq内部有4个默认的队里,在发送消息时,同一组的消息需要按照顺序,发送到相应的mq中,同一组 ...
- TZOJ 3710 修路问题(最小差值生成树kruskal或者LCT)
描述 xxx国“山头乡”有n个村子,政府准备修建乡村公路,由于地形复杂,有些乡村之间可能无法修筑公路,因此政府经过仔细的考察,终于得到了所有可能的修路费用数据.并将其公布于众,广泛征求村民的修路意见. ...
- Python: 浅淡Python中的属性(property)
起源:项目过程中需要研究youtube_dl这个开源组件,翻阅其中对类的使用,对比c#及Delphi中实现,感觉Python属性机制挺有意思.区别与高级编程语言之单一入口,在类之属性这一方面,它随意的 ...
- echarts中间有字饼图Demo1
echarts链接:http://gallery.echartsjs.com/editor.html?c=xHy2vIPzLQ 代码: option = { backgroundColor: 'bla ...
- golang 简单的实现内 网 穿 透,用户访问本地服务。
一.功能描述: 客户端通过访问外网服务器上指定端口,间接访问自已本地的内网服务. 二.原理图如下: 三.实现代码如下: server.go代码: package main; import ( &quo ...
- Oracle高级查询之OVER
注释:为了方便大家学习和测试,所有的例子都是在Oracle自带用户Scott下建立的 oracel的高级用法:rank()/dense_rank() over(partition by ...orde ...