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 ...
随机推荐
- undefined reference to symbol' pthread_create@@GLIBC_2.2.5'
我在ubuntu16.04上迁移工程,遇到了这个错误. pthread库不是Linux系统默认的库,链接时需要添加-pthread参数. 这里注意是链接那一步添加-pthread,而不是编译选项.
- JS中取得<asp:TextBox中的值
var s = document.getElementById("<%=txt_DaShen.ClientID %>").value; 注:txt_DaShen 为as ...
- oracle中的to_number在mysql中的转换
select cast(11 as unsigned int) /*整型*/ select cast(11 as decimal(10,2)) /*浮点型*/ 注:(10,2)代表数字共十位,小数点后 ...
- Windows到Linux的文件上传、下载
1.使用我们常用的Xshell登录工具,新建立一个远程会话,填写ip地址及用户名密码后,选择最下面的ZMODEM,填写下载的路径.加载的路径:2个路径可以一样也可以不一样: 2.在Linux主机上,安 ...
- python学习-类属性和实例属性
#类属性和实例属性 class Tool(object): #类属性 total = 0 #静态方法 @classmethod def say(self): print("hello wor ...
- jsplumb踩坑
一,,关于连线器label 我们全局设置中可以用 getInstance 实例化新对象 也可以通过 importDefaults 实例化新对象 当我想要给连线器上添加标签 每个连线器上的标签都不一样 ...
- sql backup
create or replace procedure P_updateasbegin update security_price p set p.closing_price = (select MI ...
- C#使用MiniDump捕获异常
c/c++语言里MiniDump是一个重要的调试手段,他们没有C#/java这样语言有很多异常输出信息( JVM异常导出bug日志功能,通常在jdk目录,文件格式hs_err_%pid%.log,pi ...
- idea spring-boot gradle mybatis 搭建开发环境
使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot.mybatis 1.新建项目 说明:1.src为源码路径,开发主要在src下 2.src/main/jav ...
- 用js实现回车登录而不用点击登录按钮
在你的登录jsp里面,添加一个js <script> function on_return(){ //on_return这是方法名 if(window.event.keyCode == 1 ...