当前位置:网站首页>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];
}边栏推荐
- Grounding relay dd-1/60
- How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
- JS扁平化数形结构的数组
- APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- One click filtering to select Baidu online disk files
- How to avoid JVM memory leakage?
- 注释与注解
- buuctf-pwn write-ups (8)
- Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
猜你喜欢

ansys命令

How to avoid JVM memory leakage?

测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文

Kubernets first meeting

Grounding relay dd-1/60

Weekly summary (*63): about positive energy

Notes and notes

Leakage detection relay jy82-2p

How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below

Upper computer software development - log information is stored in the database based on log4net
随机推荐
How to avoid JVM memory leakage?
JSON Web Token----JWT和传统session登录认证对比
How much computing power does transformer have
webrtc 快速搭建 视频通话 视频会议
How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below
ansys命令
Leakage detection relay jy82-2p
Lightroom import picture gray / Black rectangular multi display
BUU-Crypto-[HDCTF2019]basic rsa
[microservice] Nacos cluster building and loading file configuration
JS arguments parameter usage and explanation
JS execution mechanism
Experience weekly report no. 102 (July 4, 2022)
JS how to convert seconds into hours, minutes and seconds display
2022.7.2-----leetcode. eight hundred and seventy-one
JSON web token -- comparison between JWT and traditional session login authentication
Upper computer software development - log information is stored in the database based on log4net
C语言中的函数(详解)
Google Chrome browser will support the function of selecting text translation
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout