-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1473_min_cost.cc
52 lines (48 loc) · 1.51 KB
/
1473_min_cost.cc
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
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
using namespace::std;
class Solution {
public:
int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {
const int inf = 1000010;
vector<vector<vector<int>>> dp(m + 1, vector<vector<int>> (target + 1, vector<int> (n + 1, inf)));
for (int k = 1; k <= n; ++k)
dp[0][0][k] = 0;
for (int i = 1; i <= m; ++i) {
int color = houses[i - 1];
for (int j = 1; j <= min(target, i); ++j) {
if (color) {
dp[i][j][color] = dp[i - 1][j][color];
for (int k = 1; k <= n; ++k) {
if (k != color)
dp[i][j][color] = min(dp[i][j][color], dp[i - 1][j - 1][k]);
}
continue;
}
for (int k = 1; k <= n; ++k) {
dp[i][j][k] = dp[i - 1][j][k];
for (int z = 1; z <= n; ++z) {
if (z != k)
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][z]);
}
dp[i][j][k] += cost[i - 1][k - 1];
}
}
}
int res = inf;
for (int i = 1; i <= n; ++i)
res = min(res, dp[m][target][i]);
return res < inf ? res : -1;
}
};
int main()
{
return 0;
}