当前位置:网站首页>LeetCode_746 使用最小花费爬楼梯
LeetCode_746 使用最小花费爬楼梯
2022-04-21 20:07:00 【W__winter】
1、题目:使用最小花费爬楼梯
给你一个整数数组 cost ,其中 cost[i] 是从楼梯第 i 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。
你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。
请你计算并返回达到楼梯顶部的最低花费。

2、解题思路
动规五部曲
1、确定dp数组以及下标的含义,到达第i个台阶所花费的最少体力为dp[i]
2、确定递推公式,可以有两个途径得到dp[i],一个是dp[i-1] 一个是dp[i-2]。选其中最小的,所以dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
3、dp数组如何初始化,看一下递归公式,dp[i]由dp[i-1],dp[i-2]推出,既然初始化所有的dp[i]是不可能的,那么只初始化dp[0]和dp[1]就够了,其他的最终都是dp[0]dp[1]推出。
vector<int> dp(cost.size());
dp[0] = cost[0];
dp[1] = cost[1];
4、确定遍历顺序,dp[i]又dp[i-1]dp[i-2]推出,所以是从前到后遍历cost数组就可以了。
5、举例推导dp数组,cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] ,来模拟一下dp数组的状态变化,如下:



3、代码
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int> dp(cost.size());
dp[0] = cost[0];
dp[1] = cost[1];
for (int i = 2; i < cost.size(); i++) {
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
}
// 注意最后一步可以理解为不用花费,所以取倒数第一步,第二步的最少值
return min(dp[cost.size() - 1], dp[cost.size() - 2]);
}
};
版权声明
本文为[W__winter]所创,转载请带上原文链接,感谢
https://blog.csdn.net/W__winter/article/details/124329914
边栏推荐
- 【暑期实习】字节客户端一面
- Introduction to applet project files
- 824. Goat Latin
- First acquaintance with EEMBC coremark
- MySQL error 2005
- Interface non idempotent solution
- getchar,putchar,EOF
- [play with lighthouse] use Tencent cloud to realize wechat payment business in light weight
- Jerry's interrupt list [chapter]
- HW - new employee examination - traversal
猜你喜欢

Comprehensive solution for digital construction of double prevention mechanism in hazardous chemical enterprises

URL to download network resources

Know that Chuangyu issued a heavy strategic plan to build a practical defense system for continuous exchange of fire

联想公布ESG新进展:承诺2025年全线计算机产品100%含有再生塑料

What exactly is a vector?

Employment of college students in the "most difficult employment season": more than half of the graduates have landed, and higher vocational colleges produce sweet pastries
![[gradle] problem analysis + download and installation + environment configuration + installation verification](/img/a6/d6ea65127fbbed259f5f22faf23839.png)
[gradle] problem analysis + download and installation + environment configuration + installation verification

CUDA02 - 访存优化和Unified Memory

Digital business cloud: analyze the current situation of enterprise procurement management and promote the optimization and upgrading of enterprise procurement mode

Code out efficiency Chapter 7 concurrency and multithreading
随机推荐
Jerry's low power sleep [chapter]
How to judge whether the nbit bit of int type value is 1 or 0
VIM from dislike to dependence (6) -- insertion mode
Enterprise cross-border e-commerce platform service solution, cross-border e-commerce trade business framework construction, operation and maintenance
redis
Efficient C language memory copy Test result Rand, loop, operator =% in x86-64 SUSE
80. (leaflet chapter) leaflet calls the PostGIS data layer published by GeoServer
【verbs】使用ibverbs api注意事项|libibverbs 中 fork() 支持的状态如何?
Three implementation methods of quick sorting
快速排序的三种实现方式
How to make join run faster? (book at the end of the text)
Gbase 8A set group_ concat_ max_ Solution to error reporting after len parameter
C# 双保险进程监视器 lol 保证被监视的程序'几乎'永远运行. 关键字:进程操作 进程查看 创建进程
How to check the slow response of the system with high CPU?
1075 pat judge (25 points)
Redis Foundation
【原】通俗说法所谓数码相机的“动态像素”和“静态像素”背后的故事
Cuda02 - memory access optimization and unified memory
Jerry's VDDIO_ SYSVDD_ DC14 system voltage configuration description [chapter]
C# 版本的 計時器類 精確到微秒 秒後保留一比特小數 支持年月日時分秒帶單比特的輸出