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

最小花费爬楼梯

https://www.nowcoder.com/practice/6fe0302a058a4e4a834ee44af88435c7

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param cost int整型vector 
     * @return int整型
     */
    int minCostClimbingStairs(vector<int>& cost) {
        // write code here
        int sum[100000];
        int i =2;
        sum[0] = cost[0];
        sum[1] = cost[1];
        while(i<=cost.size())
        {
            sum[i] = cheaper(sum[i-1]+cost[i],sum[i-2]+cost[i]);
            i++;
        }
        return sum[cost.size()]-cost[cost.size()];
    }
    int cheaper(int a,int b)
    {
        return a>b?b:a;
    }
};

全部评论

相关推荐

09-01 17:26
已编辑
门头沟学院
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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