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 ...
随机推荐
- window git bash客户端vimrc设置tab缩进
从开发机写的代码,弄到windows上的客户端git提交,总是显示格式对不起的问题,问题是再vimrc上tab键的缩进不等于4个空格,然后就需要设置成和linux一样的四格缩进. 安装上git bas ...
- 洛谷 P1042 乒乓球
P1042 乒乓球 var s:string; a1:array[1..50000] of char; i,n,x,y:longint; procedure f1; begin while not e ...
- struts2的@Result annotation 如何添加params,并且在页面取值
http://www.bubuko.com/infodetail-2492575.html .............................................. 标签:lai ...
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- Java集合框架常见面试题
点击关注公众号及时获取笔主最新更新文章,并可免费领取本文档配套的<Java面试突击>以及Java工程师必备学习资源. 剖析面试最常见问题之Java基础知识 说说List,Set,Map三者 ...
- android开发学习 ------- 【转】 android中的单例模式 (详解)
https://blog.csdn.net/u011418943/article/details/60139644 这篇文章 前因后果 都说出来了 ,值得学习. https://blog.cs ...
- mongodb备份、还原
1.mongodb 备份 mongodump 命令可以使用可用的选项的列表. 此命令将仅备份指定的数据库,在指定的路径 语法 描述 示例 mongodump --host HOST_NAME --p ...
- Mysql order by 多字段排序
mysql单个字段降序排序: select * from table order by id desc; mysql单个字段升序排序: select * from table order by id ...
- Oracle查询排序asc/desc 多列 order by
查询结果的排序 显示EMP表中不同的部门编号. 如果要在查询的同时排序显示结果,可以使用如下的语句: SELECT 字段列表 FROM 表名 WHERE 条件 ORDER BY 字段名1 [ASC|D ...
- 用户登陆界面(jquery)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...