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:
cost
will 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 ...
随机推荐
- mysql 5.6 安装x64版本
从mysql下载的 32bit的 msi installer 后 , 安装的时候没有发现选择x64的地方, 最后安装完毕后发现server是32bit的,只能删除server( installer ...
- poj3276 Face The Right Way
Face The Right Way POJ - 3276 题目大意: n头牛排成一列,每头牛向前或向后,为了让所有牛都面向前方,设定一个k值,每操作一次恰好使k头连续的牛转向,求最少的操作次数m和对 ...
- spoj3105 MOD - Power Modulo Inverted(exbsgs)
传送门 关于exbsgs是个什么东东可以去看看yyb大佬的博客->这里 //minamoto #include<iostream> #include<cstdio> #i ...
- G - You Are the One(需要重想一遍)
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- redis 的操作
redis Redis(Remote Dictionary Server)是一个开源使用的非关系型数据库 通常被称为数据结构服务器,因为值可以是字符串,哈希,列表,集合,有序集合 优势 性能极高, ...
- Maximum Control (medium) Codeforces - 958B2
https://codeforces.com/contest/958/problem/B2 题解:https://www.cnblogs.com/Cool-Angel/p/8862649.html u ...
- foreach循环与迭代器循环 删除插入元素的区别
(1)仅对其遍历而不修改容器大小时,只使用foreach循环 (2)需要边遍历边修改容器大小时(插入删除元素),只使用迭代器循环 import java.util.HashMap;import ja ...
- 转 如何诊断和解决high version count 10.2.0.4 and 11.2.0.4
转自 http://blog.csdn.net/notbaron/article/details/50927492 在Oracle 10g以上的版本,High version count可谓是一个臭名 ...
- 配置Gradle构建
构建基础配置 Android Studio包含一个顶级的构建文件和每个模块的构建文件.构建文件被称为 build.gradle,它是一个纯文本文件,它使用Groovy语法来配置由Android Gra ...
- 安卓新的联网方式 Volley的使用(2)
如果使用volley 获取 网络图片 最好还是用 Volley 提供的NetworkImageView类, 可以很轻松的 完成工作, 而且他可以设置缓存, lru 和 sd卡的缓存.一些都封装好了. ...