当前位置:网站首页>牛客网:最小花费爬楼梯
牛客网:最小花费爬楼梯
2022-06-30 15:44:00 【lsgoose】


这个很明显要用动态规划来做,首先,我们设一个动态规划的数组dp[n+1],其中n是楼梯数,dp[i]表示到达第i个台阶的时候最小的花费。设置一个cost[n]来存储爬某一个楼梯要用的代价。第一个楼梯的代价是cost[n-1],以此类推。
接下来列出动态转移方程,很明显,我们要么在之前跨了两级,要么只跨了一级。如果是跨了两级。
dp[i]=min(dp[i-2]+cost[i-2],dp[i-1]+cost[i-1])
代码如下所示:
#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;
}边栏推荐
- Does flinkcdc have to be a clustered version if the monitored database is Mongo
- 猎头5万挖我去VC
- 中国传奇教授李泽湘,正在批量制造独角兽
- Asp. NETCORE uses cache and AOP to prevent repeated commit
- Siyuan notes: can you provide shortcut keys for folding all titles on the page?
- 几百行代码实现一个 JSON 解析器
- 容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
- Two methods for MySQL to open remote connection permission
- Lecun points out the direction of next generation AI: autonomous machine intelligence
- 思源笔记:能否提供页面内折叠所有标题的快捷键?
猜你喜欢

Distributed machine learning: model average Ma and elastic average easgd (pyspark)

ArcMap operation series: 80 plane to latitude and longitude 84

Policy Center-User Data

Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world

Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022

MySQL transaction / lock / log summary

实时渲染和预渲染有什么区别

Interview experience of service end test engineer
MySQL8.0开启远程连接权限的方法步骤

Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
随机推荐
思源笔记:能否提供页面内折叠所有标题的快捷键?
MySQL8.0开启远程连接权限的方法步骤
附加:(还没写,别看~~~)WebMvcConfigurer接口;
Openresty built in variable
Mysql代理中间件Atlas安装和配置
备战数学建模34-BP神经网络预测2
招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
Simulation of two-color ball system to judge the winning situation
服务端测试工程师面试经验
Phone number shielding function
flinkcdc如果监控的数据库为mongo就必须是集群版吗
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
RTP 发送PS流零拷贝方案
附加:(还没写,别看~~~)CorsFilter过滤器;
新茶饮“死去活来”,供应商却“盆满钵满”?
Map reduce case super detailed explanation
What role does "low code" play in enterprise digital transformation?
Cesium-1.72 learning (earth model creation online offline tile)
Arcmap操作系列:80平面转经纬度84
Simulate user login function