-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode256.java
28 lines (26 loc) · 874 Bytes
/
leetcode256.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Solution: DP
Formula:
for the i+1 house:
curRed = min(lastGreen, lastBlue) + cost[i+1][0]
curGreen = min(lastRed, lastBlue) + cost[i+1][1]
curBlue = min(lastRed, lastGreen) + cost[i+1][2]
*/
public class Solution {
public int minCost(int[][] costs) {
if(costs == null || costs.length == 0)
return 0;
int lastRed = costs[0][0];
int lastGreen = costs[0][1];
int lastBlue = costs[0][2];
for(int i = 0; i < costs.length; i++){
int curRed = Math.min(lastGreen, lastBlue) + cost[i+1][0];
int curGreen = Math.min(lastRed, lastBlue) + cost[i+1][1];
int curBlue = Math.min(lastRed, lastGreen) + cost[i+1][2];
int lastRed = curRed;
int lastGreen = curGreen;
int lastBlue = curBlue;
}
return Math.min(Math.min(lastRed, lastGreen), lastBlue);
}
}