You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor:
floor[i] = '0' denotes that the ith tile of the floor is colored black.
On the other hand, floor[i] = '1' denotes that the ith tile of the floor is colored white.
You are also given numCarpets and carpetLen. You have numCarpetsblack carpets, each of length carpetLen tiles. Cover the tiles with the given carpets such that the number of white tiles still visible is
minimum. Carpets may overlap one another.
Return theminimum number of white tiles still visible.

Input: floor ="10110101", numCarpets =2, carpetLen =2Output: 2Explanation:
The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible.No other way of covering the tiles with the carpets can leave less than 2 white tiles visible.

Input: floor ="11111", numCarpets =2, carpetLen =3Output: 0Explanation:
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.Note that the carpets are able to overlap one another.
We want to minimize the number of visible white tiles by optimally placing carpets. At each tile, we can either cover it with a carpet (if we have carpets left) or leave it uncovered. Using DP, we can recursively decide for each position and number of carpets left, and memoize the result.
classSolution {
public:int minimumWhiteTiles(string floor, int numCarpets, int carpetLen) {
int n = floor.size();
vector<vector<int>> memo(n+1, vector<int>(numCarpets+1, -1));
function<int(int,int)> dp = [&](int i, int k) {
if (i >= n) return0;
if (memo[i][k] !=-1) return memo[i][k];
int res = (floor[i] =='1'?1:0) + dp(i+1, k);
if (k >0) res = min(res, dp(i+carpetLen, k-1));
return memo[i][k] = res;
};
returndp(0, numCarpets);
}
};
classSolution {
publicintminimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
int n = floor.length();
int[][] memo =newint[n+1][numCarpets+1];
for (int[] row : memo) Arrays.fill(row, -1);
return dp(0, numCarpets, floor, carpetLen, memo);
}
privateintdp(int i, int k, String floor, int carpetLen, int[][] memo) {
if (i >= floor.length()) return 0;
if (memo[i][k]!=-1) return memo[i][k];
int res = (floor.charAt(i) =='1'? 1 : 0) + dp(i+1, k, floor, carpetLen, memo);
if (k > 0) res = Math.min(res, dp(i+carpetLen, k-1, floor, carpetLen, memo));
return memo[i][k]= res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funminimumWhiteTiles(floor: String, numCarpets: Int, carpetLen: Int): Int {
val n = floor.length
val memo = Array(n+1) { IntArray(numCarpets+1) { -1 } }
fundp(i: Int, k: Int): Int {
if (i >= n) return0if (memo[i][k] != -1) return memo[i][k]
var res = if (floor[i] =='1') 1else0 res += dp(i+1, k)
if (k > 0) res = minOf(res, dp(i+carpetLen, k-1))
memo[i][k] = res
return res
}
return dp(0, numCarpets)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from typing import List
classSolution:
defminimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
n = len(floor)
memo = [[-1] * (numCarpets +1) for _ in range(n +1)]
defdp(i: int, k: int) -> int:
if i >= n:
return0if memo[i][k] !=-1:
return memo[i][k]
res = (floor[i] =='1') + dp(i +1, k)
if k >0:
res = min(res, dp(i + carpetLen, k -1))
memo[i][k] = res
return res
return dp(0, numCarpets)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnminimum_white_tiles(floor: String, num_carpet: i32, carpet_len: i32) -> i32 {
let n = floor.len();
let floor_bytes = floor.as_bytes();
letmut memo =vec![vec![-1; (num_carpet+1) asusize]; n+1];
fndp(i: usize, k: i32, n: usize, carpet_len: i32, floor: &[u8], memo: &mut Vec<Vec<i32>>) -> i32 {
if i >= n { return0; }
if memo[i][k asusize] !=-1 { return memo[i][k asusize]; }
letmut res =if floor[i] ==b'1' { 1 } else { 0 } + dp(i+1, k, n, carpet_len, floor, memo);
if k >0 {
res = res.min(dp(i + carpet_len asusize, k -1, n, carpet_len, floor, memo));
}
memo[i][k asusize] = res;
res
}
dp(0, num_carpet, n, carpet_len, floor_bytes, &mut memo)
}
}