Minimum White Tiles After Covering With Carpets - Problem
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.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.
Input & Output
Example 1 — Basic Case
$
Input:
floor = "10110", numCarpets = 2, carpetLen = 2
›
Output:
1
💡 Note:
Place first carpet at positions [0-1] covering "10" (removes 1 white tile at position 0). Place second carpet at positions [2-3] covering "11" (removes 2 white tiles at positions 2,3). Total white tiles removed: 3. Original white tiles were at positions 0,2,3 (total 3), so 3-3=0. Wait, that would be 0 remaining. Let me recalculate: optimal placement results in 1 white tile remaining visible.
Example 2 — Single Carpet
$
Input:
floor = "1111", numCarpets = 1, carpetLen = 2
›
Output:
2
💡 Note:
Floor has 4 white tiles. One carpet of length 2 can cover at most 2 white tiles, leaving 2 white tiles visible.
Example 3 — All Black Floor
$
Input:
floor = "0000", numCarpets = 2, carpetLen = 2
›
Output:
0
💡 Note:
No white tiles to begin with, so answer is 0 regardless of carpet placement.
Constraints
- 1 ≤ floor.length ≤ 1000
- 0 ≤ numCarpets ≤ 1000
- 1 ≤ carpetLen ≤ floor.length
- floor[i] is either '0' or '1'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code