forked from super30admin/DP-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaintHouse.java
More file actions
29 lines (23 loc) · 1005 Bytes
/
PaintHouse.java
File metadata and controls
29 lines (23 loc) · 1005 Bytes
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
29
// Time Complexity : O(n) n=rows=houses.
// Space Complexity : O(m) m=colors=costs[0].length;
// Did this code successfully run on Leetcode :yes.
// Any problem you faced while coding this :
// Your code here along with comments explaining your approach
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Paint House.
//Memory Usage: 39.3 MB, less than 94.12% of Java online submissions for Paint House.
public int minCost(int[][] costs) {
if (costs==null || costs.length==0)
return 0;
int dp[]= new int[costs[0].length];
for (int i=0;i<costs[0].length;i++){
dp[i]=costs[0][i];
}
for (int i=1;i<costs.length;i++){
int prevDP0=dp[0];
int prevDP1=dp[1];
dp[0]=costs[i][0]+Math.min(prevDP1,dp[2]);
dp[1]=costs[i][1]+Math.min(prevDP0,dp[2]);
dp[2]=costs[i][2]+Math.min(prevDP0,prevDP1);
}
return Math.min(dp[0],Math.min(dp[1],dp[2]));
}