作者: 负雪明烛
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:

  1. cost will have a length in the range [2, 1000].
  2. 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)的更多相关文章

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

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

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

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

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

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

    题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...

  6. Leetcode 746. Min Cost Climbing Stairs

    思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...

  7. 【Leetcode_easy】746. Min Cost Climbing Stairs

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

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

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

随机推荐

  1. PPT插件——iSlide全功能解析

    做幻灯展示是我们日常工作中不可缺少的一部分,有的人喜欢用代码如Latex, markdown+pandoc+revealjs 或 bookdown.这些都是自动化做幻灯的好工具.我也都有过体会,确实简 ...

  2. Python函数之传参

    目录 1. 函数传参 1.1 参数的作用 1.2 形参和实参 1.3 位置参数 1.4 关键字参数 1.5 默认实参 1.6 参数总结 2. 可变参数 1. 函数传参 1.1 参数的作用 1.2 形参 ...

  3. Excel-条件判断

    5.条件判断 IFS(条件1,真1,假1-条件2,真2,假2-条件n,真n,假n-条件n+1,...,TRUE,执行)   #可以嵌套164个(大概!具体忘了) IF(条件1,真,假)

  4. EXCEL-对筛选出(单独手动隐藏行还是在统计范围内)的表格数据进行统计

    =SUBTOTAL(3,A1:A5)  #计算筛选出的表格中A1:A5中有几个值. =SUBTOTAL(3,I71:I21447)  ,在I71:I21447之间计数,会自动略去没有筛选上的隐藏单元格 ...

  5. 在VS2008环境下编写C语言DLL,并在C++和C#项目下调用 (转载)

    1.编写DLL a)文件--打开--新建项目--Win32,右侧Win32项目,填写好项目名称,点击"下一步", 应用程序类型选择:"DLL(D)",附加选项: ...

  6. .NET Core如何配置TLS Cipher(套件)?

    前言 前不久我发表了一篇关于TLS协议配置被我钻了空子,经过第三方合作伙伴验证,针对此TLS协议存在不安全套件,急催速速解决,那么我们本篇开始继续整活!第三方合作伙伴对平台安全严苛要求,我们已连续发版 ...

  7. 自动化测试系列(三)|UI测试

    UI 测试是一种测试类型,也称为用户界面测试,通过该测试,我们检查应用程序的界面是否工作正常或是否存在任何妨碍用户行为且不符合书面规格的 BUG.了解用户将如何在用户和网站之间进行交互以执行 UI 测 ...

  8. 论文翻译:2020_Weighted speech distortion losses for neural-network-based real-time speech enhancement

    论文地址:基于神经网络的实时语音增强的加权语音失真损失 论文代码:https://github.com/GuillaumeVW/NSNet 引用:Xia Y, Braun S, Reddy C K A ...

  9. C语言之内核中的struct list_head 结构体

    以下地址文章解释很好 http://blog.chinaunix.net/uid-27122224-id-3277511.html 对下面的结构体分析 1 struct person 2 { 3 in ...

  10. SQLITE_BTREE_H

    sqlite3.c, 237436行 = 全部源文件,找东西比多文件查找方便多了:-),字符串查找一点都不慢. 不要太害怕,SQLite说它的代码里有非常多是用来做数据完整性检查和测试的.但愿B树,虚 ...