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 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:
costwill have a length in the range[2, 1000].- 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的更多相关文章
- 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 ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 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 ...
- [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 ...
- Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)
题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...
- [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 ...
- [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 ...
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 746. Min Cost Climbing Stairs 最不费力的加权爬楼梯
[抄题]: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...
随机推荐
- ComDom在使用函数CompileAssemblyFromFile时无法找到文件的错误
public virtual CompilerResults CompileAssemblyFromFile( CompilerParameters options, params string[] ...
- Oracle Java SE 组件概念图
JDK1.8 组件概念图
- 企业级应用,如何实现服务化二(dubbo架构)
这是企业级应用,如何实现服务化系列的第二篇.在上一篇:企业级应用,如何实现服务化一(项目架构演化)中,交代了企业级应用架构的演化过程,和服务治理的方案可以选择:dubbo,或者spring cloud ...
- java面试基础问题
1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.Java有 ...
- 在Mac上安装Ubuntu14.04虚拟机
1.在macOS High Sierras上安装VMware for mac 下载地址:VMware Fusion 8.5.1 https://pan.baidu.com/s/1skQ1OyL 2. ...
- pwnhub 相对路径覆盖
这个pwnhub小m师傅的题,做的时候完全没有思路. 首先是注册然后可以看到一个加载css的地方,是相对路径加载(当然我并没有觉得有什么问题). 服务端和浏览器解析URL是有区别的,就是%2f 服务器 ...
- Java EE学习笔记(五)
Spring事务管理 1.Spring事务管理概述 1).在实际开发中,操作数据库时都会涉及到事务管理问题,为此Spring提供了专门用于事务处理的API.(事务特性:ACID,原子性,一致性,隔离性 ...
- SNMP消息传输机制
1.引言 基于TCP/IP的网络管理包含3个组成部分: 1) 一个管理信息库MIB(Management Information Base).管理信息库包含所有代理进程的所有可被查询和修改的参数.RF ...
- Java微服务 进程间通信
目录 进程间通信 同步调用 异步调用 实现方式 进程间通信 同步调用 同步调用比较简单,一致性强,但是容易出调用问题,出现单点故障,因为之间相互依赖,比如RPC必须要依赖的模块上线可用,己方才能调用, ...
- Linux下自动化测试环境的搭建
1.安装Linux虚拟机,详情参考 https://blog.csdn.net/qq_22770715/article/details/78558374 https://www.cnblog ...