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 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:
Input: cost = [10, 15, 20] Output: 15 Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
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].
Solution:
class Solution: def minCostClimbingStairs(self, cost): """ :type cost: List[int] :rtype: int """ start_0 = 0 start_1 = 0 for each in cost[::-1]: old_start_1 = start_1 start_1 = start_0 start_0 = min(start_0 + each, old_start_1 + each) return min(start_0, start_1)
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/42032.html
摘要:同时你可以选择从第阶开始或者第一阶开始。我们的目标是找出你爬到最顶层所付出的最小的开销。最低开销是,从第阶开始,只花费就可以到顶层。想法动态规划问题。的长度应该为数组长度加一,其中数组的最后一个元素的值就是本题的答案,最低开销。 题目详情 On a staircase, the i-th step has some non-negative cost cost[i] assigned ...
摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...
摘要:递归法复杂度时间空间思路这题几乎就是求解斐波那契数列。最简单的方法就是递归。但重复计算时间复杂度高。代码递推法复杂度时间空间思路实际上我们求的时候只需要和的值,所以可以减少一些空间啊。 Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c...
摘要:实质就是把之前出现过的中间结果记录,下次再出现相同情况的时候,通过可以只用的时间复杂度得到。表示到达第层楼梯的不同走法。那么题目中每次可以选择走一步,或者两步,。从迭代公式可以知道,有两个,和。 70. Climbing Stairs You are climbing a staircase. It takes n steps to reach to the top.Each tim...
Problem A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. Ex...
阅读 844·2019-08-30 15:54
阅读 3315·2019-08-29 15:33
阅读 2700·2019-08-29 13:48
阅读 1212·2019-08-26 18:26
阅读 3332·2019-08-26 13:55
阅读 1475·2019-08-26 10:45
阅读 1163·2019-08-26 10:19
阅读 304·2019-08-26 10:16