问题描述:

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

思路:

考虑动态规划算法,依次遍历每一个元素,都计算包含该元素和不包含该元素的结果。

代码:

 class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
notadd , add = 0 , 0
for i in cost:
notadd , add = add , i + min(notadd, add)
return min(notadd , add)

Python3解leetcode Min Cost Climbing Stairs的更多相关文章

  1. [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

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

  2. Min Cost Climbing Stairs - LeetCode

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

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

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

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

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

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

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

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

  8. 【Leetcode_easy】746. Min Cost Climbing Stairs

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

  9. Min Cost Climbing Stairs [746]

    Min Cost Climbing Stairs [746] 题目描述 简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以 ...

随机推荐

  1. Vue知识整理4:v-html标签

    可以在数据绑定中使用html标签,这样在变量里可以使用html标签输出结果,如下所示:

  2. CentOS7设置启动模式问题

    参考地址 https://www.linuxidc.com/Linux/2015-12/126356.htm

  3. Delphi XE2 之 FireMonkey 入门(12) - 动画(上)

    在 HD 窗体上添加一个 TAniIndicator, 修改其 Enabled 属性为 True, 动画完成了. 这是最简单的动画相关的控件了, 只有两个值得注意的属性: Enabled: Boole ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_10 斗地主案例(双列)_1_斗地主案例的需求分析

    之前做的斗地主的版本,没有从小到大进行排序 一个存储牌的花色,一个存储牌的序号. 放牌的容器.使用Map 再创建一个集合进行洗牌. 调用shuffer方法洗牌.生成后就是随即的索引了.

  5. mybatis初步理解

    mybatis概念   mybatis 是一款轻量级的orm的数据持久框架,封装jdbc 对开发提供了便利,但是性能会比jdbc低,从开发的角度来说,现在是比较流行的 掌握上比较容易,也支持缓存,级联 ...

  6. Oracle-第一篇一些调优技巧

    1.查询 1>通过提示,使用索引. 2>使用/*+parallel*/并行查询 3>查看执行计划,调整sql语句或者优化表结构 4>避免使用“*”号 2.表设计:partiti ...

  7. free野指针问题

    gdb backtrace内容如下: Program received signal SIGABRT, Aborted. (gdb) p cmd No symbol "cmd" i ...

  8. websocket无法注入bean问题解决方案

    websocket服务端往往需要和服务层打交道,因此需要将服务层的一些bean注入到websocket实现类中使用,但是呢,websocket实现类虽然顶部加上了@Component注解,依然无法通过 ...

  9. more - 在显示器上阅读文件的过滤器

    总览 (SYNOPSIS) more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ] 描述 (DESCRIPTION) More 是 ...

  10. 01. Linux-Kali系统网卡配置

    配置网卡操作: vim /etc/network/interfaces auto eth0iface eth0 inet static 配置eth0使用默认的静态地址address 192.168.1 ...