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

最小花费爬楼梯

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

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param cost int整型一维数组 
 * @param costLen int cost数组长度
 * @return int整型
 */

#include <math.h>
int min ( int a , int b ) {
    return ( a < b ) ? a : b ;
}

int minCostClimbingStairs(int* cost, int costLen ) {
    int dp[100005];
    memset ( dp , 0 , sizeof (dp));
    // dp[i] 表示 爬到 第 i 层所花费的最小费用
    int n = costLen;
    for (int i = 2; i <= n; i ++ ) {
        dp[i] = min ( dp[i-1] + cost[i-1] , dp[i-2] + cost[i-2]);
    }

    return dp[n];
}

全部评论

相关推荐

985柜员:开发还敢还叫,全部让自测就老实了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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