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

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

Note:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].

方法一:正序

/**
* @param cost 每一步所要花费的值
* @return 到达顶部总共需要的值
*/
public int minCostClimbingStairs(int[] cost) {
int length = cost.length + 1;
int[] dp = new int[length];
dp[0] = 0;
dp[1] = 0;
for (int i = 2; i < length; i++) {
dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);
}
return dp[length - 1];
}

  

方法二:倒序

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int f1 = , f2 = ;
for (int i=cost.size()-; i>= ; i--){
int f0 = cost[i] + min(f1, f2);
f2 = f1;
f1 = f0;
}
return min(f1, f2);
}
};

【easy】746. Min Cost Climbing Stairs 动态规划的更多相关文章

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

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

  3. 【Leetcode_easy】746. Min Cost Climbing Stairs

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

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

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

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

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

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

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

  9. Leetcode 746. Min Cost Climbing Stairs

    思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...

随机推荐

  1. To B Vs To C

    谈谈 To B 业务的难点https://tangjie.me/blog/259.html

  2. 关于echarts.js 柱形图

    echarts.js官网: http://www.echartsjs.com/index.html 这是我所见整理最详细echarts.js 柱形图博客: https://blog.csdn.net/ ...

  3. React笔记:组件(3)

    1. 组件定义 组件是React的核心概念,组件将应用的UI拆分成独立的.可复用的模块. 定义组件的两种方式: (1)类组件:使用ES6 class (2)函数组件:使用函数 使用class定义组件的 ...

  4. MariaDB多实例的安装配置

    初始化数据库: mysql_install_db  --basedir=/var/lib/mysql --datadir=/data/3306/data --user=mysql mysql_inst ...

  5. Msi中文件替换

    转自https://blog.csdn.net/davidhsing/article/details/9962377 ※说明:目前可以用于MSI编辑的软件很多,但是有些软件在保存时会在MSI文件中写入 ...

  6. ADO.Net笔记整理(一)

    几次装机,Notes已烟消云散,近日因为Node.js死活搞不定,无奈装机,备份好的东东,没想到磁盘扇区出现异常,可能是PE启动盘的病毒,只好将磁盘全部重新分区,恢复数据也懒得恢复了,日积月累关乎将来 ...

  7. [luogu1600]NOIp2016D1T2 天天爱跑步

    题目链接: luogu1600 谨以此题纪念那段年少无知但充满趣味的恬淡时光 附上一位dalao的博客链接:https://www.luogu.org/blog/user26242/ke-pa-di- ...

  8. TCP/IP的四元组、五元组、七元组

    TCP/IP的四元组.五元组.七元组 四元组是: 源IP地址.目的IP地址.源端口.目的端口 五元组是: 源IP地址.目的IP地址.协议号.源端口.目的端口 七元组是: 源IP地址.目的IP地址.协议 ...

  9. CF_528D

    一句话题意 给你两个串s.t,长度为n.m,字符集为"ATGC",当且仅 当[i - k; i + k]中存在一个j,使得s[j ] = t[x]时,s[i ]可以 和t[x]匹配 ...

  10. 关于SDK_JDK_JRE_JVM的关系

    SDK JDK JRE JVM 四者的关系 一:SDK与JDK的关系(可以认为jdk只是sdk的一种子集) SDK是Software Development Kit的缩写,中文意思是“软件开发工具包” ...