problem

746. Min Cost Climbing Stairs

题意:

solution1:动态规划;

定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i]如何推导。思考一下如何才能到第i层呢?是不是只有两种可能性,一个是从第i-2层上直接跳上来,一个是从第i-1层上跳上来。不会再有别的方法,所以dp[i]只和前两层有关系,所以可以写做如下:

dp[i] = min(dp[i- 2] + cost[i - 2], dp[i - 1] + cost[i - 1])

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n+, );
for(int i=; i<=n; ++i)//errr..
{
dp[i] = min(dp[i-]+cost[i-], dp[i-]+cost[i-]);
}
return dp[n];
}
};

solution2:

要爬当前的台阶,肯定需要加上当前的cost[i],那么还是要从前一层或者前两层的台阶上跳上来,那么选择dp值小的那个,所以递归式如下:

dp[i] = cost[i] + min(dp[i- 1], dp[i - 2])

最后在最后两个dp值中选择一个较小的返回即可

class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
vector<int> dp(n, );
dp[] = cost[], dp[] = cost[];
for(int i=; i<n; ++i)//errr..
{
dp[i] = cost[i] + min(dp[i-], dp[i-]);
}
return min(dp[n-], dp[n-]);
}
};

参考

1. Leetcode_easy_746. Min Cost Climbing Stairs;

2. Grandyang;

【Leetcode_easy】746. Min Cost Climbing Stairs的更多相关文章

  1. 【easy】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 t ...

  2. 【Leetcode】746. Min Cost Climbing Stairs

    题目地址: https://leetcode.com/problems/min-cost-climbing-stairs/description/ 解题思路: 官方给出的做法是倒着来,其实正着来也可以 ...

  3. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

  9. [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 ...

随机推荐

  1. HaspMap和ConcurrentHashMap

    参考自:http://www.importnew.com/28263.html HaspMap和ConcurrentHashMap(康科瑞特哈希迈普) Java7 HashMap 不支持并发操作,Ha ...

  2. vue jqury如何获取元素中的属性

    1.点击事件获取 点击事件通过传值得方式 <el-button type="danger" round @click="delHander($event)" ...

  3. JAVA的变量,数据类型与运算符

    1. 变量 计算机处理数据,变量被用来存储处理的数据,之所以叫做变量因为你可以改变存储的值.更确切的说,一个变量指向着一块存储特定类型值的地址,换句话说,一个变量有名称.类型和值.一个变量有一个名称, ...

  4. leetcode解题报告(17):Missing Number

    描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mis ...

  5. F 阎小罗的Minimax (第十届山东理工大学ACM网络编程擂台赛 正式赛 )

    题解:by Mercury_Lc 阎小罗的矩阵给的n和m都不超过300,枚举一下所有情况就可以了,用前缀和来储存.数组a[x][y]代表前x行前y列的和是多少,那么枚举每一种切割的方式就可以.注意一下 ...

  6. local模式运行spark-shell时报错 java.lang.IllegalArgumentException: Error while instantiating 'org.apache.spark.sql.hive.HiveSessionState':

    先前在local模式下,什么都不做修改直接运行./spark-shell 运行什么问题都没有,然后配置过在HADOOP yarn上运行,之后再在local模式下运行出现以下错误: java.lang. ...

  7. P1069 细胞分裂——数学题,质因数分解

    P1069 细胞分裂 我们求的就是(x^k)|(m1^m2) k的最小值: 先给m1分解质因数,再给每个细胞分解: 如果m1有的质因数,细胞没有就跳过: 否则就记录答案: 注意整数除法下取整的原则: ...

  8. Cubic-bezier 曲线

    cubic-bezier又称三次贝塞尔,主要是为animation生成速度曲线函数. cubic-bezier(x1,y1,x2,y2) 此图中: P0:(0,0) P1:(x1,y1) P2:(x2 ...

  9. Android中View大小的确定过程

    View and ViewGroup 安卓中有5种基本的 ViewGroup: FrameLayout RelativeLayout LinearLayout TableLayout Absolute ...

  10. IdentityServer4入门五:错误处理

    在访问ClientMvc的保护页面时,会跳转到IdentityMvc页面,这时会出现类似下图的错误界面,让人无从入手. 如果你尝试按文字所说的内容去处理.你发现项目已正确设置.其实上面的内容是固定的, ...