1. """
  2. On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
  3. 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.
  4. Example 1:
  5. Input: cost = [10, 15, 20]
  6. Output: 15
  7. Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
  8. Example 2:
  9. Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
  10. Output: 6
  11. Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
  12. """
  13. """
  14. 此题为简单的动态规划,按部就班算cost[i],即可找出动态方程
  15. 参考leetcode198
  16. """
  17. class Solution1(object):
  18. def minCostClimbingStairs(self, cost):
  19. n = len(cost)
  20. if n == 0:
  21. return 0
  22. if n == 1:
  23. return cost[0]
  24. if n == 2:
  25. return min(cost[0], cost[1])
  26. for i in range(2, n):
  27. cost[i] = min(cost[i-1]+cost[i], cost[i-2]+cost[i])
  28. #bug 动态方程写为了min(cost[i-1], cost[i-2]+cost[i])
  29. return min(cost[n-1], cost[n-2]) #!!!因为是爬到山顶,返回值在数组中

leetcode746 Min Cost Climbing Stairs的更多相关文章

  1. LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)

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

  2. Leetcode746.Min Cost Climbing Stairs使用最小花费爬楼梯

    数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯. 您需 ...

  3. Min Cost Climbing Stairs - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...

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

  6. LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11

    746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...

  7. 【Leetcode_easy】746. Min Cost Climbing Stairs

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

  8. Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)

    Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...

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

随机推荐

  1. nginx 的 content阶段的root指令与alias指令

    root 与alias指令 Syntax: alias path; Default: — Context: location Syntax: root path; Default: root html ...

  2. Spring学习(二)

    IoC 1.Inverse of Control ,控制反转(控制权的翻转) 2.控制:对对象的创建.对对象的属性赋值等一系列操作本来应该是我们做的事情 Java Application : Date ...

  3. 什么是Socket:

    先了解一些前提: 网络由下往上分为 物理层 .数据链路层 . 网络层 . 传输层 . 会话层 . 表现层 和 应用层.通过初步了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对 ...

  4. Java基础 -2.2

    整形数据 保存的范围由小到大:byte.short.int.long 在java中任何的一个整型常量 那么其默认的类型都是int型(只要是整数就是int类型的数据). public class ex1 ...

  5. Ubuntu安装TTF字体

    sudo mkdir /usr/share/fonts/ttf sudo cp ~/ttf/* /usr/share/fonts/ttf cd /usr/share/fonts/ttf sudo ch ...

  6. Python - Mro

    参考 https://stackoverflow.com/questions/2010692/what-does-mro-do http://python.jobbole.com/85685/ 问题: ...

  7. gcc/g++/make/cmake/makefile/cmakelists的恩恩怨怨

    以前在windows下用VS写代码,不管有多少个文件夹,有多少个文件,写完以后只需要一键就什么都搞定了.但是当移步linux下时,除非你使用图形界面,并且使用Qt creater这类的IDE时,才可以 ...

  8. 微信小程序 画布arc截取圆形图片

    画布提供了一种可以创建圆的方法 arc(x, y, r, s, e, counterclockwise) x,y:圆心 r:圆的半径 s:起始弧度 (0) e:终止弧度 (1.5 * Math.PI) ...

  9. VNC怎么和宿主机共享粘贴板

    VNC怎么和宿主机共享粘贴板 假设目标主机是linux,终端主机是windows(就是在windows上使用VNC登陆linux) 在linux中执行vncconfig -nowin& 在li ...

  10. css元素隐藏方式

    1.opacity:设置一个元素的透明度 .hide {opacity: 0;} 2.visibility:设置一个元素可见\不可见.hide {visibility: hidden} .hide { ...