两种方法,核心思想都一样,求出走到每一步上的最小开销,直到最后一步和倒数第二步,比较其最小值返回即可。

方法一,用一个辅助的容器

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

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

  2. 【Leetcode_easy】746. Min Cost Climbing Stairs

    problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...

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

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

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

  6. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

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

  8. 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯

    [抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

  9. 【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 ...

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

随机推荐

  1. viewpager fragment 滑动界面

    先新建几个fragment,包括java和xml 然后在主界面的布局文件中: <android.support.v4.view.ViewPager android:id="@+id/m ...

  2. linux下面redis安装

    安装方法1redis1.下载安装包2.解压程序包tar -zxvf  redis-3.2.6.tar.gz3.编译源程序make(编译失败,查看是否安装gcc   如果没有yum install gc ...

  3. 安装tensorflow ubuntu18.04

    1.首先安装环境是ubuntu18.04. $sudo apt-get install python-pip python-dev python-virtualenv2.安装virtualenv虚拟环 ...

  4. kraken-ejs创建一个项目【学习札记】

    Keep in Touch. 保持联络. Who’s calling? 是哪一位? You did right. 你做得对. You set me up! 你出卖我! kraken-express-e ...

  5. C++ 单例模式(懒汉、饿汉模式)

    1.简单的单例模式实现 2.C++的构造函数不是线程安全的,所以上述代码在多线程的情况下是不安全的,原因是new Singelton时,这句话不是原子的,比如一个线程执行了new的同时,另一个线程对i ...

  6. 解决在Mac的Vmware Fusion中装win7系统和mac原生系统直接切换win7系统分辨率变化的问题

    虚拟机 - 设置 - 显示屏 - 全屏显示retina (此选项钩去掉)

  7. echarts柱状图Demo

    echarts链接:http://gallery.echartsjs.com/editor.html?c=xB1Sfo5JbX 代码: var xData = ['a', 'b', 'c', 'd', ...

  8. VS2008水晶报表变两页(重装系统后)

    找到水晶报表中的设置-打印机设置,发现打印机名称是 Microsoft XPS Document Writer (已重定向2),

  9. 弹出PopupWindow背景变暗的实现

    弹出PopuoWindow后 代码里设置的是PopupWindow默认获取焦点 所以PopupWindow显示的时候其它控件点击是没有反应的 用到的方法是 pwMyPopWindow.setFocus ...

  10. MATLAB单步调试

    我用的是matlab R2012b 1.先设置断点:点击菜单栏中"EDITOR"--"Breakpoints"--"set",出现以下对话框 ...