问题描述:

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

思路:

考虑动态规划算法,依次遍历每一个元素,都计算包含该元素和不包含该元素的结果。

代码:

 class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
notadd , add = 0 , 0
for i in cost:
notadd , add = add , i + min(notadd, add)
return min(notadd , add)

最新文章

  1. ubuntu安装mysql
  2. 关于从Activity A跳转到Activity B ,其中Activity A中有一个VideoView,Activity B中有一个MediaPlayer。
  3. C++ 应用处理异常崩溃(转载)
  4. python---常见的数据队列
  5. js-FCC算法-Pairwise
  6. Delphi XE5 android 获取网络状态
  7. <转>键盘扫描码
  8. [编解码] 关于base64编码的原理及实现
  9. NGUI 实现 透明底图遮罩 && 人物像素变黑
  10. wamp虚拟机配置
  11. 密码算法详解——DES
  12. Android 全屏方法
  13. Java静态代码块、构造代码块执行顺序问题
  14. QEMU KVM Libvirt手册(7): 硬件虚拟化
  15. python开发环境_windows系统安装_错误记录
  16. 2018.5.3 docker
  17. MQTT研究之EMQ:【EMQ之HTTP认证/访问控制】
  18. nginx 禁止未绑定的域名访问
  19. [译]C#7 Pattern Matching
  20. Microsoft SQL - 数据库管理系统

热门文章

  1. JS text节点无innerHTML
  2. 类Thread
  3. 数组的includes方法
  4. C/C++ 引入头文件时 #include<***.h> 与 #include"***.h" 区别
  5. Maven构建Struts2框架的注意事项
  6. Socket服务端和客户端文件传输
  7. Vue源码详细解析:transclude,compile,link,依赖,批处理...一网打尽,全解析!
  8. sde自动备份到文件gdb
  9. UI标签库专题十:JEECG智能开发平台 Form(form标签)
  10. MySQL5.7 慢查询+DDL操作堵塞查询