Python3解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 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].
思路:
考虑动态规划算法,依次遍历每一个元素,都计算包含该元素和不包含该元素的结果。
代码:
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的更多相关文章
- [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 ...
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
- LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11
746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- 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 ...
- 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 ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- Min Cost Climbing Stairs [746]
Min Cost Climbing Stairs [746] 题目描述 简单来说就是:要跳过当前楼梯需要花费当前楼梯所代表的价值cost[i], 花费cost[i]之后,可以选择跳一阶或者两阶楼梯,以 ...
随机推荐
- Vue实现音乐播放器(四):页面入口+header组件的编写
首先下载三个包 babel-runtime对es语法进行转义 fastclick解决移动端点击300毫秒延迟的问题 babel-polyfill对es6 api进行转义 下载了包之后要在main.js ...
- centos7安装kafka
1.官网或 wget 下载 kafka_2.12-2.2.0.tgz 二进制代码包 cd /home/tar wget http://mirror.bit.edu.cn/apache/kafka/2. ...
- gitlab+jenkins之环境搭建
文中用到的安装包都已经上传到百度网盘,下载地址在文章底部(还没传...) 前置条件: 为了节约配置时间,在正式配置前,应该先做好如下准备: 首先先把整个流程仔仔细细的看3遍,确认对配置整体的流程,配置 ...
- CentOS7设置启动模式问题
参考地址 https://www.linuxidc.com/Linux/2015-12/126356.htm
- 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第1节 异常_1_异常概念&异常体系
Throwable是可抛出的意思.
- Prometheus Alertmanager Grafana 监控警报
Prometheus Alertmanager Grafana 监控警报 #node-exporter, Linux系统信息采集组件 #prometheus , 抓取.储存监控数据,供查询指标 #al ...
- 删除历史日志的一个API
删除历史日志的一个API bool DeleteOldFiles(const char* strFolder, const char* strPrefix, bool is_recursion, UI ...
- 基于Quartz.net 的任务调度平台Weiz.TaskManager
Weiz.TaskManager https://github.com/weizhong1988/Weiz.TaskManager 任务管理平台 系统简介 Quartz.net是一个开源的任务调度工具 ...
- js-禁止长页面滚动
标题的需求问题其实我经常遇到.尤其是在碰到页面同时出现有视频及弹层的情况. 当然我说的问题皆是针对微信H5开发的哈 IOS中,视频播放,弹层出现时,视频在弹层的下面,不会出现问题: 安卓手机中,完了, ...
- 不起眼的vim.转自https://blog.csdn.net/iplayvs2008/article/details/51508599
如果我的关于这个话题的最新帖子没有提醒到你的话,那我明确地说,我是一个 Vim 的粉丝.所以在你们中的某些人向我扔石头之前,我先向你们展示一系列“鲜为人知的 Vim 命令”.我的意思是,一些你可能以前 ...