lc 746 Min Cost Climbing Stairs


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 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:

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

DP Accepted

dp[i]代表从i起跳所需要付出的最小代价,很明显dp[0] = cost[0],且dp1 = cost1,对于i >= 2的情况,dp[i] = min(dp[i-1] + cost[i], dp[i-2] + cost[i]),即跳到i点的那一步要么是一步跳要么是两步跳,取最小值,而这道题的答案很明显就是min(dp[cost.size()-1], dp[cost.size()-2])。

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

最新文章

  1. 移动端rem实现响应布局
  2. junit单元测试中私有方法测试
  3. CentOS6.4安装Smokeping节点监控软件
  4. 制作C/C++动态链接库(dll)若干注意事项
  5. p3p之讲解
  6. Python之回调魔法
  7. 全部与精简切换显示jQuery实例教程
  8. jQuery mouseover,mouseout事件多次执行的问题处理
  9. C# 打开指定文件或网址
  10. webpack+vue-cil 中proxyTable配置接口地址代理
  11. 如何注册Filter
  12. javascript面向对象习题答案
  13. Beta冲刺 2
  14. [luogu2822][组合数问题]
  15. 获取字段唯一值工具- -ArcPy和Python案例学习笔记
  16. python核心编程笔记——Chapter2
  17. Python实现QQ自动点赞
  18. Windows下ftp服务器搭建及配置
  19. bzoj1257: [CQOI2007]余数之和 整除分块
  20. Let it crash philosophy part II

热门文章

  1. 蓝牙协议(bluetooth spec)
  2. HDU3709 Balanced Number —— 数位DP
  3. HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举
  4. [Selenium] Grid 介绍
  5. 【hdu 5418】 Victor and world
  6. Watir: 当出现错误提示AutoItX3.dll 没有注册的时候,该怎么处理?
  7. Spring中的扩展点
  8. 整体二分 HDU - 5808
  9. Ant Design Vue项目解析-前言
  10. AppBoxFuture(七): 分布式外键约束