题解 | #最小花费爬楼梯#

最小花费爬楼梯

https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int min_cost(int num,int* cost)
{
    int dp1=0,dp2=0,dp=0;
    for (int i=2; i<=num; i++) {
    dp=min(dp1+cost[i-1],dp2+cost[i-2]); 
    dp2=dp1;
    dp1=dp;
    } 
    return dp;
}



int main() {
    int n;
    cin>>n;
    int cost[n];
    for (int i=0; i<n; i++) {
    cin>>cost[i];
    }
    int output;
   output=min_cost(n, cost);
    cout<<output;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务