当前位置:网站首页>746. Climb stairs with minimum cost
746. Climb stairs with minimum cost
2022-07-04 06:01:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/min-cost-climbing-stairs/
subject :
Give you an array of integers cost , among cost[i] It's from the stairs i The cost of climbing a step up . Once you pay this fee , You can choose to climb up one or two steps .
You can choose from the subscript to 0 Or subscript 1 The steps began to climb the stairs .
Please calculate and return the minimum cost to reach the top of the stairs .
Example 1:
| Input :cost = [10,15,20] Output :15 explain : You will subscript 1 The steps begin . - payment 15 , Climb up two steps , Get to the top of the stairs . The total cost is 15 . |
Example 2:
| Input :cost = [1,100,1,1,1,100,1,1,100,1] Output :6 explain : You will subscript 0 The steps begin . - payment 1 , Climb up two steps , The arrival subscript is 2 The steps of . - payment 1 , Climb up two steps , The arrival subscript is 4 The steps of . - payment 1 , Climb up two steps , The arrival subscript is 6 The steps of . - payment 1 , Climb up a step , The arrival subscript is 7 The steps of . - payment 1 , Climb up two steps , The arrival subscript is 9 The steps of . - payment 1 , Climb up a step , Get to the top of the stairs . The total cost is 6 . |
Tips :
| 2 <= cost.length <= 1000 0 <= cost[i] <= 999 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/min-cost-climbing-stairs
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
This is (LeetCode-70) climb stairs A derivative version of , We use the idea of dynamic programming to deal with
1. The first i The cost of a step is dp[i]
2. The first i The next step is from i-1 perhaps i-2 Up the steps , That is to say
dp[i-1] Add the cost of this step up perhaps
dp[i-2] Add the cost of this step up
3. Then the formula can be set as :
dp[i] = min(dp[i-1] + cost[i-1] , dp[i-2] + cost[i-2]);
4. Initial element settings
dp[0] = 0; dp[1] = 0;
These two are the initial stages , It hasn't started to go up yet , So there is no cost
The topic gives a range of expenses :2 <= cost.length <= 1000
that dp[10001] That's all right.
Method 1 、 Dynamic programming
#define min(a,b) ( (a) < (b) ? (a) : (b) )
int minCostClimbingStairs(int* cost, int costSize){
int i;
int dp[1001]={0,0};
for(i=2; i<=costSize; i++)
{
dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]);
}
return dp[costSize];
}边栏推荐
- High performance parallel programming and optimization | lesson 02 homework at home
- JS execution mechanism
- Qt发布多语言国际化翻译
- buuctf-pwn write-ups (8)
- AWT常用组件、FileDialog文件选择框
- transformer坑了多少算力
- Arc135 a (time complexity analysis)
- Google Chrome browser will support the function of selecting text translation
- 509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
- 复合非线性反馈控制(二)
猜你喜欢
![BUU-Crypto-[GXYCTF2019]CheckIn](/img/b8/ad6c05977f6943f30e9975acb6eb6e.jpg)
BUU-Crypto-[GXYCTF2019]CheckIn

AWT introduction

How to configure static IP for Kali virtual machine

C语言练习题(递归)

LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout

QT get random color value and set label background color code

JS how to convert seconds into hours, minutes and seconds display

如何实现视频平台会员多账号登录

体验碎周报第 102 期(2022.7.4)

19. Framebuffer application programming
随机推荐
My NVIDIA developer journey - optimizing graphics card performance
The difference between PX EM rem
ANSYS command
JS execution mechanism
JSON web token -- comparison between JWT and traditional session login authentication
剑指 Offer II 038. 每日温度
fastjson
Canoe panel learning video
复合非线性反馈控制(二)
云原生架构实战案例及优化解决方案
Notes and notes
Design and implementation of redis 7.0 multi part AOF
js获取对象中嵌套的属性值
ES6 模块化
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
How to expand all collapse panels
SQL injection - injection based on MSSQL (SQL Server)
Webrtc quickly set up video call and video conference
如何避免 JVM 内存泄漏?
1480. Dynamic sum of one-dimensional array