在一个小城市里,有 m
个房子排成一排,你需要给每个房子涂上 n
种颜色之一(颜色编号为 1
到 n
)。有的房子去年夏天已经涂过颜色了,所以这些房子不可以被重新涂色。
我们将连续相同颜色尽可能多的房子称为一个街区。(比方说 houses = [1,2,2,3,3,2,1,1]
,它包含 5 个街区 [{1}, {2,2}, {3,3}, {2}, {1,1}]
。)
给你一个数组 houses
,一个 m * n
的矩阵 cost
和一个整数 target
,其中:
houses[i]
:是第i
个房子的颜色,0 表示这个房子还没有被涂色。cost[i][j]
:是将第i
个房子涂成颜色j+1
的花费。
请你返回房子涂色方案的最小总花费,使得每个房子都被涂色后,恰好组成 target
个街区。如果没有可用的涂色方案,请返回 -1 。
示例 1:
输入:houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 输出:9 解释:房子涂色方案为 [1,2,2,1,1] 此方案包含 target = 3 个街区,分别是 [{1}, {2,2}, {1,1}]。 涂色的总花费为 (1 + 1 + 1 + 1 + 5) = 9。
示例 2:
输入:houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 输出:11 解释:有的房子已经被涂色了,在此基础上涂色方案为 [2,2,1,2,2] 此方案包含 target = 3 个街区,分别是 [{2,2}, {1}, {2,2}]。 给第一个和最后一个房子涂色的花费为 (10 + 1) = 11。
示例 3:
输入:houses = [0,0,0,0,0], cost = [[1,10],[10,1],[1,10],[10,1],[1,10]], m = 5, n = 2, target = 5 输出:5
示例 4:
输入:houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 输出:-1 解释:房子已经被涂色并组成了 4 个街区,分别是 [{3},{1},{2},{3}] ,无法形成 target = 3 个街区。
提示:
m == houses.length == cost.length
n == cost[i].length
1 <= m <= 100
1 <= n <= 20
1 <= target <= m
0 <= houses[i] <= n
1 <= cost[i][j] <= 10^4
方法一:动态规划
我们定义
接下来,我们从下标
如果未涂色,那么我们可以将下标为
如果已经涂色,那么我们可以将下标为
最后,我们返回
时间复杂度
class Solution:
def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int:
f = [[[inf] * (target + 1) for _ in range(n + 1)] for _ in range(m)]
if houses[0] == 0:
for j, c in enumerate(cost[0], 1):
f[0][j][1] = c
else:
f[0][houses[0]][1] = 0
for i in range(1, m):
if houses[i] == 0:
for j in range(1, n + 1):
for k in range(1, min(target + 1, i + 2)):
for j0 in range(1, n + 1):
if j == j0:
f[i][j][k] = min(
f[i][j][k], f[i - 1][j][k] + cost[i][j - 1])
else:
f[i][j][k] = min(
f[i][j][k], f[i - 1][j0][k - 1] + cost[i][j - 1])
else:
j = houses[i]
for k in range(1, min(target + 1, i + 2)):
for j0 in range(1, n + 1):
if j == j0:
f[i][j][k] = min(f[i][j][k], f[i - 1][j][k])
else:
f[i][j][k] = min(f[i][j][k], f[i - 1][j0][k - 1])
ans = min(f[-1][j][target] for j in range(1, n + 1))
return -1 if ans >= inf else ans
class Solution {
public int minCost(int[] houses, int[][] cost, int m, int n, int target) {
int[][][] f = new int[m][n + 1][target + 1];
final int inf = 1 << 30;
for (int[][] g : f) {
for (int[] e : g) {
Arrays.fill(e, inf);
}
}
if (houses[0] == 0) {
for (int j = 1; j <= n; ++j) {
f[0][j][1] = cost[0][j - 1];
}
} else {
f[0][houses[0]][1] = 0;
}
for (int i = 1; i < m; ++i) {
if (houses[i] == 0) {
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= Math.min(target, i + 1); ++k) {
for (int j0 = 1; j0 <= n; ++j0) {
if (j == j0) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j][k] + cost[i][j - 1]);
} else {
f[i][j][k]
= Math.min(f[i][j][k], f[i - 1][j0][k - 1] + cost[i][j - 1]);
}
}
}
}
} else {
int j = houses[i];
for (int k = 1; k <= Math.min(target, i + 1); ++k) {
for (int j0 = 1; j0 <= n; ++j0) {
if (j == j0) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j][k]);
} else {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j0][k - 1]);
}
}
}
}
}
int ans = inf;
for (int j = 1; j <= n; ++j) {
ans = Math.min(ans, f[m - 1][j][target]);
}
return ans >= inf ? -1 : ans;
}
}
class Solution {
public:
int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {
int f[m][n + 1][target + 1];
memset(f, 0x3f, sizeof(f));
if (houses[0] == 0) {
for (int j = 1; j <= n; ++j) {
f[0][j][1] = cost[0][j - 1];
}
} else {
f[0][houses[0]][1] = 0;
}
for (int i = 1; i < m; ++i) {
if (houses[i] == 0) {
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= min(target, i + 1); ++k) {
for (int j0 = 1; j0 <= n; ++j0) {
if (j == j0) {
f[i][j][k] = min(f[i][j][k], f[i - 1][j][k] + cost[i][j - 1]);
} else {
f[i][j][k] = min(f[i][j][k], f[i - 1][j0][k - 1] + cost[i][j - 1]);
}
}
}
}
} else {
int j = houses[i];
for (int k = 1; k <= min(target, i + 1); ++k) {
for (int j0 = 1; j0 <= n; ++j0) {
if (j == j0) {
f[i][j][k] = min(f[i][j][k], f[i - 1][j][k]);
} else {
f[i][j][k] = min(f[i][j][k], f[i - 1][j0][k - 1]);
}
}
}
}
}
int ans = 0x3f3f3f3f;
for (int j = 1; j <= n; ++j) {
ans = min(ans, f[m - 1][j][target]);
}
return ans == 0x3f3f3f3f ? -1 : ans;
}
};
func minCost(houses []int, cost [][]int, m int, n int, target int) int {
f := make([][][]int, m)
const inf = 1 << 30
for i := range f {
f[i] = make([][]int, n+1)
for j := range f[i] {
f[i][j] = make([]int, target+1)
for k := range f[i][j] {
f[i][j][k] = inf
}
}
}
if houses[0] == 0 {
for j := 1; j <= n; j++ {
f[0][j][1] = cost[0][j-1]
}
} else {
f[0][houses[0]][1] = 0
}
for i := 1; i < m; i++ {
if houses[i] == 0 {
for j := 1; j <= n; j++ {
for k := 1; k <= target && k <= i+1; k++ {
for j0 := 1; j0 <= n; j0++ {
if j == j0 {
f[i][j][k] = min(f[i][j][k], f[i-1][j][k]+cost[i][j-1])
} else {
f[i][j][k] = min(f[i][j][k], f[i-1][j0][k-1]+cost[i][j-1])
}
}
}
}
} else {
j := houses[i]
for k := 1; k <= target && k <= i+1; k++ {
for j0 := 1; j0 <= n; j0++ {
if j == j0 {
f[i][j][k] = min(f[i][j][k], f[i-1][j][k])
} else {
f[i][j][k] = min(f[i][j][k], f[i-1][j0][k-1])
}
}
}
}
}
ans := inf
for j := 1; j <= n; j++ {
ans = min(ans, f[m-1][j][target])
}
if ans == inf {
return -1
}
return ans
}
func min(a, b int) int {
if a < b {
return a
}
return b
}