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

题目地址: Min Cost Climbing Stairs

难度: Easy

题意: 

(1)从第一个元素或者第二个元素位置出发

(2)每次可以走一步或者走两步

(3)每到一个位置,都要收"过路费"

(4)返回需要的最少"过路费"

思路:

根据上面的第二个例子分析:

一个元素:  [1]   过路费为1
两个元素: [1, 100] 位置0 -> 跳出 过路费为1
三个元素: [1, 100, 1] 位置0 -> 位置2 -> 跳出 过路费为2
四个元素: [1, 100, 1, 1] 位置0 -> 位置2 -> 跳出 过路费为2
五个元素: [1, 100, 1, 1, 1] 位置0 -> 位置2 -> 位置3(或者位置4) -> 跳出 过路费为3
六个元素: [1, 100, 1, 1, 1, 100] 位置0 -> 位置2 -> 位置4 -> 跳出 过路费为3
七个元素: [1, 100, 1, 1, 1, 100, 1] 位置0 -> 位置2 -> 位置4 -> 位置6 -> 跳出 过路费4
八个元素: [1, 100, 1, 1, 1, 100, 1, 1] 位置0 -> 位置2 -> 位置4 -> 位置6 -> 跳出 过路费4
九个元素: [1, 100, 1, 1, 1, 100, 1, 1, 100] 位置0 -> 位置2 -> 位置4 -> 位置6 -> 位置7 -> 跳出 过路费5
十个元素: [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] 位置0 -> 位置2 -> 位置4 -> 位置6 -> 位置7 -> 位置9 -> 跳出 过路费6

采用动态规划方式,  dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])

代码:

class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
step = [0] * (len(cost) + 1 )
i = 2
if len(cost) == 1:
return cost[0]
if len(cost) == 2:
return min(cost)
while i <= len(cost):
step[i] = min(step[i-1]+cost[i-1], step[i-2]+cost[i-2])
i += 1
return step[-1]

时间复杂度: O(n)

空间复杂度: O(n)

746. Min Cost Climbing Stairs@python的更多相关文章

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

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

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

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

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

  8. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

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

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

随机推荐

  1. 如何使得 python 脚本 不一闪而过

    1.  简单的方法是在最后加上如下语句: os.system("pause") 2. 但是这个不一定有用,原因是可能在之前的代码中发生异常,那么我们看到的效果也是直接一闪而过 办法 ...

  2. bzoj 1901: Zju2112 Dynamic Rankings【整体二分+树状数组||主席树+树状数组】

    整体二分: 对于每一个修改操作,标记为1,并且加一个标记为-1的这个位置原来值,并且对于a数列每个点都当成修改操作 然后整体二分,扫当前操作区间lr,把在值域区间标记为1和-1的操作都在树状数组对应位 ...

  3. 2014-5-10 NOIP模拟赛 by coolyangzc

    Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走,厉害吧. 早苗的新模型可以按照输入的 ...

  4. 买票案例 1.synchronize关键字 2.lock锁

  5. struts工作原理

    在struts2的应用中,从用户请求到服务器返回相应响应给用户端的过程中,包含了许多组件如:Controller.ActionProxy.ActionMapping.Configuration Man ...

  6. 应用日志获取-web系统

    1 场景 应用使开发写的,但应用使部署再服务器上,而开发没有ssh登陆服务器的权限. so,开发总是请运维查日志,下载日志. so and so,运维要花很多时间帮开发去搞日志. 这是件很没意义的事, ...

  7. rtos概要

    一 RTOS如何调试: 静态调试帮不上忙,因为嵌入式系统都是动态系统 ,要借助基于RTOS系统的可视化分析 :Micriµm 的 µC/Probe ,SEGGER 的 SystemView ,Perc ...

  8. HDU 3359 高斯消元模板题,

    http://acm.hdu.edu.cn/showproblem.php?pid=3359 题目的意思是,由矩阵A生成矩阵B的方法是: 以a[i][j]为中心的,哈曼顿距离不大于dis的数字的总和 ...

  9. Linux--NiaoGe-Service-07网络安全与主机基本防护

    Linux系统内自带的防火墙有两层: 第一层:数据包过滤防火墙:IP Filtering和Net Filter 要进入Linux本机的数据包都会先通过Linux预先内置的防火墙(Net Filter) ...

  10. Mex(线段树的巧妙应用)

    题目要求求某段区间第一个没有出现的数(0,1,2,3....) ,对于所有的区间,我们把这样的数加起来最后得到一个结果. 首先,我们要求出这样的数,然后还得列举出所有的区间,复杂度太大了. 换种思路, ...