-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1473_PaintHouseIII.cpp
124 lines (101 loc) · 3.77 KB
/
LC_1473_PaintHouseIII.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
https://leetcode.com/problems/paint-house-iii/
1473. Paint House III
*/
class Solution {
public:
vector<int> housesMat;
vector<vector<int>> costMat;
int numHouses;
int numColors;
int target;
vector<vector<vector<int>>> memo;
int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {
this->housesMat = move(houses);
this->costMat = move(cost);
numHouses = m;
numColors = n;
this->target = target;
memo.resize(numHouses+1, vector<vector<int>>(numColors+1, vector<int>(target+1, -1)));
memo[0][0][0] = solve(0, 0, 0);
return memo[0][0][0] == 1e7 ? -1 : memo[0][0][0];
}
// id of cur house, color of the prev house, neighbourhood count
int solve(int idx, int prevcolor, int nghcnt)
{
if(nghcnt > target)
return 1e7;
if(idx == numHouses) // all houses covered
{
if(nghcnt == target) // target reached and houses covered
return memo[idx][prevcolor][nghcnt]=0;
else
return 1e7; // target not reached
}
if(memo[idx][prevcolor][nghcnt] != -1)
return memo[idx][prevcolor][nghcnt];
int cst = 1e7;
int curColor = housesMat[idx];
if(curColor != 0) // house is already painted
{
if(prevcolor == curColor)
cst = solve(idx+1, curColor, nghcnt);
else
cst = solve(idx+1, curColor, nghcnt+1);
}
else // house is not painted
{
for(int col=1; col<= numColors; col++)
{
if(prevcolor == col)
cst = min(cst, solve(idx+1, col, nghcnt) + costMat[idx][col-1]);
else
cst = min(cst, solve(idx+1, col, nghcnt+1) + costMat[idx][col-1]);
}
}
return memo[idx][prevcolor][nghcnt]=cst;
}
/*
int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {
this->housesMat = move(houses);
this->costMat = move(cost);
numHouses = m;
numColors = n;
this->target = target;
memo.resize(numHouses+1, vector<vector<int>>(numColors+1, vector<int>(target+1, -1)));
memo[0][0][0] = solve(0, 0, 0);
return memo[0][0][0] == 1e7 ? -1 : memo[0][0][0];
}
int solve(int idx, int prevcolor, int nghcnt)
{
if(nghcnt > target || idx > numHouses)
return 1e7;
if(nghcnt == target and idx == numHouses) // target reached and houses covered
return memo[idx][prevcolor][nghcnt]=0;
if(idx == numHouses) // target not reached but houses covered
return memo[idx][prevcolor][nghcnt]=1e7;
if(memo[idx][prevcolor][nghcnt] != -1)
return memo[idx][prevcolor][nghcnt];
int cst = 1e7;
int curColor = housesMat[idx];
if(curColor != 0) // house is already painted
{
if(prevcolor!=0 and prevcolor == curColor)
cst = min(cst, solve(idx+1, curColor, nghcnt));
else
cst = min(cst, solve(idx+1, curColor, nghcnt+1));
}
else
{
for(int col=1; col<= numColors; col++)
{
if(prevcolor!=0 and prevcolor == col)
cst = min(cst, solve(idx+1, col, nghcnt) + costMat[idx][col-1]);
else
cst = min(cst, solve(idx+1, col, nghcnt+1) + costMat[idx][col-1]);
}
}
return memo[idx][prevcolor][nghcnt]=cst;
}
*/
};