当前位置:网站首页>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;
}边栏推荐
- RTP 发送PS流零拷贝方案
- [unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
- 居家办公浅谈远程协助快速提效心得 | 社区征文
- [CVE-2019-0193] - Apache Solr DataImport 远程命令执行分析
- flink sql cdc 同步sqlserver 报错什么原因啊
- 招标公告:深圳市财政局数据库异地灾备项目
- Finally understand science! 200 pictures to appreciate the peak of human wisdom
- Create statement for Oracle export view
- 如何得到股票开户的优惠活动?在线开户安全么?
- [download attached] installation and use of penetration test artifact Nessus
猜你喜欢
随机推荐
RTP 发送PS流零拷贝方案
Which direction should college students choose to find jobs after graduation?
Bidding announcement: remote disaster recovery project of Shenzhen Finance Bureau database
halcon知识:矩阵专题【02】
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
Mysql代理中间件Atlas安装和配置
赛芯电子冲刺科创板:拟募资6.2亿 实控人谭健为美国籍
中国传奇教授李泽湘,正在批量制造独角兽
How cloudxr promotes the future development of XR
dart:字符串replace相关的方法解决替换字符
[cve-2019-0193] - Apache Solr dataimport remote command execution analysis
flinkcdc如果监控的数据库为mongo就必须是集群版吗
MySQL proxy middleware atlas installation and configuration
flink sql cdc 同步sqlserver 报错什么原因啊
2022 Blue Bridge Cup group B -2022- (01 backpack to calculate the number of schemes)
Dart: string replace related methods to solve replacement characters
halcon知识:区域专题【07】
为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
[附下载]渗透测试神器Nessus安装及使用









