标签:index cost min int step Cost Climbing Stairs dp
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.
可以从0或者1层开始走,每次可以选择走一层或者两层,代价是当前位置的cost
动态规划dp[i]表示从0或1楼走到第i层的时候,再往上走的最小的代价,转移方程dp[i] = min(dp[i - 1] , dp[i - 2]) + cost[i]
最后答案就是最后一层和倒数第二层,再往上走的最小代价的最小值。min(dp[n - 1], dp[n - 2])
class Solution(object): def minCostClimbingStairs(self, cost): """ :type cost: List[int] :rtype: int """ n = len(cost) dp = [0] * (n) dp[0] = cost[0] dp[1] = cost[1] for i in range(2, n, 1): dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i] return min(dp[n - 1], dp[n - 2])
标签:index,cost,min,int,step,Cost,Climbing,Stairs,dp
来源: https://www.cnblogs.com/whatyouthink/p/13254194.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。