#Leetcode# 746. Min Cost Climbing Stairs
发布日期:2022-03-12 04:49:18 浏览次数:28 分类:技术文章

本文共 1205 字,大约阅读时间需要 4 分钟。

 

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: 15Explanation: 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: 6Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

 

Note:

  1. cost will have a length in the range [2, 1000].
  2. Every cost[i] will be an integer in the range [0, 999].

代码:

class Solution {public:    int minCostClimbingStairs(vector
& cost) { int n = cost.size(); if(n == 1) return cost[0]; else if(n == 2) return min(cost[0], cost[1]); else { vector
dp(n + 1, 0); for(int i = 2; i <= n; i ++) dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); return dp[n]; } }};  

每次觉得 dp 没那么难的时候就要明白 是错觉!

跑步去啦 要减肥呢!

转载于:https://www.cnblogs.com/zlrrrr/p/10821715.html

转载地址:https://blog.csdn.net/weixin_30348519/article/details/98099596 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:报java.lang.NoClassDefFoundError: org.jbox2d.collision.AABB错误的解决办法
下一篇:我学习USB设计的历程【转】

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月07日 14时03分13秒