2311. Minimum White Tiles After Covering With Carpets¶
Difficulty: Hard
LeetCode Problem View on GitHub
2311. Minimum White Tiles After Covering With Carpets
Hard
You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor:
floor[i] = '0'denotes that theithtile of the floor is colored black.- On the other hand,
floor[i] = '1'denotes that theithtile of the floor is colored white.
You are also given numCarpets and carpetLen. You have numCarpets black 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 the minimum number of white tiles still visible.
Example 1:

Input: floor = "10110101", numCarpets = 2, carpetLen = 2 Output: 2 Explanation: 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.
Example 2:

Input: floor = "11111", numCarpets = 2, carpetLen = 3 Output: 0 Explanation: 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.
Constraints:
1 <= carpetLen <= floor.length <= 1000floor[i]is either'0'or'1'.1 <= numCarpets <= 1000
Solution¶
import java.util.Arrays;
class Solution {
private int dp[][];
private int suff[];
public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
int n = floor.length();
suff = new int[n];
int count = 0;
for (int i = n - 1; i >= 0; i--) {
if (floor.charAt(i) == '1')
count++;
suff[i] = count;
}
dp = new int[n + 1][numCarpets + 1];
for (int current[] : dp)
Arrays.fill(current, -1);
return solve(0, numCarpets, floor, carpetLen);
}
private int solve(int ind, int carpetsLeft, String floor, int carpetLen) {
if (ind >= floor.length())
return 0;
if (carpetsLeft == 0)
return suff[ind];
if (dp[ind][carpetsLeft] != -1)
return dp[ind][carpetsLeft];
int op1 = solve(ind + 1, carpetsLeft, floor, carpetLen) + (floor.charAt(ind) == '1' ? 1 : 0);
int op2 = solve(ind + carpetLen, carpetsLeft - 1, floor, carpetLen);
return dp[ind][carpetsLeft] = Math.min(op1, op2);
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here