https://www.nowcoder.com/practice/6fe0302a058a4e4a834ee44af88435c7
思路:动态规划,使用数组保存爬上下标为i的台阶的最小开销
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cost int整型vector
* @return int整型
*/
int minCostClimbingStairs(vector<int> cost) {
int len = cost.size(), dp[len];
if (len == 1) return 0;
if (len == 2) return min(cost[0],cost[1]);
// dp[i]表示爬上下标为i的台阶的最小开销
dp[0] = 0;
dp[1] = min(cost[0],cost[1]);
for (int i = 2; i < len; ++i) {
dp[i] = min(dp[i-1]+cost[i],dp[i-2]+cost[i-1]);
}
return dp[len-1];
}
};