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 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].
solution
top要么是 top - 1 上去的,要么是 top -2 上去的
故动态规划的状态转移方程为:
dp[i] = min{dp[i-1]+cost[i-1], dp[i -2]+cost[i-2]}
可以从第0阶开始,也可以从第1阶开始,故动态规划处理两次即可
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int n = cost.size();
int dp[n + 1];
dp[0] = 0;
dp[1] = cost[0];
for (int i = 2; i <= n; i++)
dp[i] = (dp[i - 1] + cost[i - 1]) < (dp[i - 2] + cost[i - 2]) ? (dp[i - 1] + cost[i - 1]) : (dp[i - 2] + cost[i - 2]);
int dp1[n + 1];
dp1[0] = 0;
dp1[1] = cost[1];
for (int i = 2; i <= n - 1; i++)
dp1[i] = (dp1[i - 1] + cost[i]) < (dp1[i - 2] + cost[i - 1]) ? (dp1[i - 1] + cost[i]) : (dp1[i - 2] + cost[i - 1]);
if (dp1[n - 1] < dp[n])
return dp1[n - 1];
else
return dp[n];
}
};
相关链接
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)的更多相关文章
- 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] 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 ...
- LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...
- Leetcode 746. Min Cost Climbing Stairs
思路:动态规划. class Solution { //不能对cost数组进行写操作,因为JAVA中参数是引用 public int minCostClimbingStairs(int[] cost) ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- Python习题集(一)
每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我! 题目 有一个数据list of dict如下 a = [ {"}, {"}, {"}, ] 写入到本 ...
- HTML,CSS,JavaScript,json,xml之间的关系
1.浏览器工作原理: https://blog.csdn.net/Luncles/article/details/80320082 2.HTML,XML,JSON之间的关系: https://blog ...
- Proteomic Profiling of Paired Interstitial Fluids Reveals Dysregulated Pathways and Salivary NID1 as a Biomarker of Oral Cavity Squamous Cell Carcinoma (解读人:张聪敏)
文献名:Proteomic Profiling of Paired Interstitial Fluids Reveals Dysregulated Pathways and Salivary NID ...
- hdu1078 dfs+dp(记忆化搜索)搜索一条递增路径,路径和最大,起点是(0,0)
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- 网维大师无盘刷新B盘方法
- python逐行读取文本
一.使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. 二.需要导入import os 三.下面是逐行读取文件内容的三种方法: ...
- 使用Python+OpenCV进行图像处理(二)| 视觉入门
[前言]图像预处理对于整个图像处理任务来讲特别重要.如果我们没有进行恰当的预处理,无论我们有多么好的数据也很难得到理想的结果. 本篇是视觉入门系列教程的第二篇.整个视觉入门系列内容如下: 理解颜色模型 ...
- vs编译器中不允许使用scanf()的情况
问题1描述: vs编译器中,不允许使用scanf()函数. error C4996: 'scanf': This function or variable may be unsafe. Conside ...
- JVM 常见参数配置
-XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+PrintGCDetails 每次触发GC的时候更详细的相关日志 -XX:+UseSerialGC 串行回收 -Xms 堆初始值( ...
- IOS 空字符串报错 解决办法
NSScanner: nil string argument NSScanner: nil string argument libc++abi.dylib: terminate_handler un ...