当前位置:网站首页>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];
}边栏推荐
- 每周小结(*63):关于正能量
- A little understanding of GSLB (global server load balance) technology
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
- 云原生架构实战案例及优化解决方案
- Webrtc quickly set up video call and video conference
- px em rem的区别
- Compound nonlinear feedback control (2)
- left_ and_ right_ Net normal version
- Steady! Huawei micro certification Huawei cloud computing service practice is stable!
- JS get the attribute values nested in the object
猜你喜欢

Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)

My NVIDIA developer journey - optimizing graphics card performance

JS扁平化数形结构的数组

如何展开Collapse 的所有折叠面板
![[untitled]](/img/32/cfd45bb5e8555ea2ad344161370dbe.png)
[untitled]

js如何将秒转换成时分秒显示
![[excel] PivotChart](/img/45/be87e4428a1d8ef66ef34a63d12fd4.png)
[excel] PivotChart

AWT介绍

Weekly summary (*63): about positive energy

Json Web token - jwt vs. Traditional session login Authentication
随机推荐
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
JS flattened array of number shape structure
注释与注解
实用的小工具指令
MySQL的information_schema数据库
[excel] PivotChart
How to avoid JVM memory leakage?
Nexus 6p从8.0降级6.0+root
MySQL information_ Schema database
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
JS execution mechanism
How to solve the component conflicts caused by scrollbars in GridView
Kubernets first meeting
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
Grounding relay dd-1/60
Compound nonlinear feedback control (2)
Penetration tool - sqlmap
left_and_right_net正常版本
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout