-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathpaint-house-iv.cpp
42 lines (41 loc) · 1.38 KB
/
paint-house-iv.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
// Time: O(n * l^4)
// Space: O(l^2)
// dp
class Solution {
public:
long long minCost(int n, vector<vector<int>>& cost) {
const int l = size(cost[0]);
vector<vector<int64_t>> dp(l, vector<int64_t>(l));
for (int k = 0; k * 2 < n; ++k) {
vector<vector<int64_t>> new_dp(l, vector<int64_t>(l, numeric_limits<int64_t>::max()));
for (int i = 0; i < l; ++i) {
for (int j = 0; j < l; ++j) {
if (j == i) {
continue;
}
for (int ni = 0; ni < l; ++ni) {
if (ni == i) {
continue;
}
for (int nj = 0; nj < l; ++nj) {
if (nj == j || ni == nj) {
continue;
}
new_dp[ni][nj] = min(new_dp[ni][nj], dp[i][j] + cost[k][ni] + cost[n - 1 - k][nj]);
}
}
}
}
dp = move(new_dp);
}
int64_t result = numeric_limits<int64_t>::max();
for (int i = 0; i < l; ++i) {
for (int j = 0; j < l; ++j) {
if (i != j) {
result = min(result, dp[i][j]);
}
}
}
return result;
}
};