Easy
一道Easy的dp题,dp题如果简单能用O(1) space的时候就尽量优化。
class Solution {
public int minCost(int[][] costs) {
if (costs == null || costs.length == 0){
return 0;
}
int n = costs.length;
int curtRed = 0, curtBlue = 0, curtGreen = 0;
for (int i = 0; i < n; i++){
int lastRed = curtRed, lastBlue = curtBlue, lastGreen = curtGreen;
curtRed = costs[i][0] + Math.min(lastBlue, lastGreen);
curtBlue = costs[i][1] + Math.min(lastRed, lastGreen);
curtGreen = costs[i][2] + Math.min(lastRed, lastBlue);
}
return Math.min(curtRed, Math.min(curtBlue, curtGreen));
}
}