当前位置:网站首页>Niuke.com: minimum cost of climbing stairs
Niuke.com: minimum cost of climbing stairs
2022-06-30 16:39:00 【lsgoose】


This obviously needs to be done with dynamic programming , First , Let's set up an array of dynamic programming dp[n+1], among n Is the number of stairs ,dp[i] It means to arrive at the third i The minimum cost of a step . Set up a cost[n] To store the cost of climbing a certain stair . The price of the first stair is cost[n-1], And so on .
Next, the dynamic transfer equations are listed , Obviously , We either crossed two levels before , Or only one level has been crossed . If you have crossed two levels .
dp[i]=min(dp[i-2]+cost[i-2],dp[i-1]+cost[i-1])
The code is as follows :
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> cost(n);
for(int i=0;i<n;++i){
cin>>cost[i];
}
vector<int> dp(n+1);
for(int i=2;i<=n;++i){
dp[i]=min(dp[i-2]+cost[i-2], dp[i-1]+cost[i-1]);
}
cout<<dp[n]<<endl;
return 0;
}边栏推荐
- Hundreds of lines of code to implement a JSON parser
- Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
- POJ Project Summer
- 牛客网:有多少个不同的二叉搜索树
- Hologres共享集群助力淘宝订阅极致精细化运营
- 电子烟强制性国家标准GB 41700-2022发布 2022年10月1日起实施
- flinkcdc如果监控的数据库为mongo就必须是集群版吗
- 中航无人机科创板上市:市值385亿 拳头产品是翼龙无人机
- MySQL master-slave configuration
- 详解Go语言中for循环,break和continue的使用
猜你喜欢

Li Zexiang, a legendary Chinese professor, is making unicorns in batches

How to connect the Internet Reading Notes - Summary

备战数学建模34-BP神经网络预测2

抖快B为啥做不好综艺

【Verilog基础】关于Clock信号的一些概念总结(clock setup/hold、clock tree、clock skew、clock latency、clock transition..)

Cesium-1.72 learning (deploy offline resources)

Policy Center > Malware > Malware

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?

15年做糊21款硬件,谷歌到底栽在哪儿?

构建适合组织的云原生可观测性能力
随机推荐
Three development trends of enterprise application viewed from the third technological revolution
RT thread heap size Setting
After 15 years of working on 21 types of hardware, where is Google?
Policy Center > Device and Network Abuse
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
附加:(还没写,别看~~~)WebMvcConfigurer接口;
【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
新茶饮“死去活来”,供应商却“盆满钵满”?
猎头5万挖我去VC
【活动报名】探秘元宇宙,就差你了!7月2号我在深圳现场等你!
BC1.2 PD协议
Installing jupyter notebook under Anaconda
I implement "stack" with C I
Policy Center > Google Play‘s Target API Level Policy
牛客网:有多少个不同的二叉搜索树
构建适合组织的云原生可观测性能力
Cesium-1.72 learning (earth model creation online offline tile)
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
云化XR,如何助力产业升级
Cesium-1.72 learning (add points, lines, cubes, etc.)