【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/min-cost-climbing-stairs/description/
题目描述
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].
题目大意
爬楼梯,每次可以走1步或者2步,求走到顶的最小花费。其中,开始的位置可以是第一个位置或者第二个位置。
解题方法
动态规划
非常初级的动态规划的问题。需要做的是另外找一个列表保存节点的路径的代价,某个节点的路径的代价等于前两个节点的路径的代价和前两个节点对应的代价之和。最后返回的结果是倒数两个节点的代价和节点值之和的最小值。
class Solution(object):
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
costed = [0, 0]
for i in xrange(2, len(cost)):
costed.append(min(costed[i - 1] + cost[i - 1], costed[i - 2] + cost[i - 2]))
return min(costed[-1] + cost[-1], costed[-2] + cost[-2])
二刷,同样的动态规划,提前把所有的dp状态声明出来,然后每一步的转移等于前两步的最小值+当前的值。因为需要上到最上面的楼梯,所以假设增加一个花费是0的楼梯。
class Solution:
def minCostClimbingStairs(self, cost):
"""
:type cost: List[int]
:rtype: int
"""
N = len(cost)
cost.append(0)
dp = [0] * (N + 1)
dp[0] = cost[0]
dp[1] = cost[1]
for i in range(2, N + 1):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
return dp[-1]
日期
2018 年 1 月 28 日
2018 年 11 月 17 日 —— 美妙的周末,美丽的天气
【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)的更多相关文章
- 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 ...
- 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] 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 ...
- LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...
- Leetcode 746. Min Cost Climbing Stairs
思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 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 ...
- [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 ...
随机推荐
- Linux—软件包管理器yum安装详解
yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器. 基於RPM包管理,能够从指定的服务器自动下载RPM包 ...
- 深度探讨 PHP 之性能
1.缘起 关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言,库类丰富,使用简单,安全,非常适合WEB开发,但性能低下.PHP的性能是否真的就 如同大家的感觉一样的差呢?本文就是围绕这么一个话题 ...
- HDFS【Namenode、SecondaryNamenode、Datanode】
目录 一. NameNode和SecondaryNameNode 1.NN和2NN 工作机制 2. NN和2NN中的fsimage.edits分析 3.checkpoint设置 4.namenode故 ...
- 前端页面存放token
//本地缓存,记录token function set(type, value) { localStorage.setItem(type, value); } function get(type) { ...
- jQuery无限载入瀑布流 【转载】
转载至 http://wuyuans.com/2013/08/jquery-masonry-infinite-scroll/ jQuery无限载入瀑布流 好久没更新日志了,一来我比较懒,二来最近也比较 ...
- 【leetcode】598. Range Addition II
You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...
- ComponentScan注解的使用
在项目初始化时,会将加@component,@service...相关注解的类添加到spring容器中. 但是项目需要,项目初始化时自动过滤某包下面的类,不将其添加到容器中. 有两种实现方案, 1.如 ...
- Output of C++ Program | Set 1
Predict the output of below C++ programs. Question 1 1 // Assume that integers take 4 bytes. 2 #incl ...
- Can namespaces be nested in C++?
In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example ...
- docker创建tomcat容器无法正常访问
记一次创建tomcat docker容器后访问是404,进入到tomcat docker容器后发现webapps是空的 1.挂载 docker run -v localConfigFile:/cont ...