再次进入动态规划的学习,先选了一道easy的题目,果然一次过。。。
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int> step;
step.resize(cost.size());
for (int i = 0; i < cost.size(); i++) {
if (i == 0 || i == 1) {
step[i] = cost[i];
} else {
step[i] = min(step[i-2]+cost[i], step[i-1]+cost[i]);
}
}
if (step[cost.size()-1] < step[cost.size()-2]) {
return step[cost.size()-1];
} else {
return step[cost.size()-2];
}
}
};