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 the ith tile of the floor is colored black.
  • floor[i] = '1' denotes that the ith tile 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
Minimum White Tiles After Covering With Carpets INPUT floor = "10110" Index: 0 1 2 3 4 1 0 1 1 0 = White (1) = Black (0) numCarpets = 2 carpetLen = 2 Available Carpets: Carpet 1 Carpet 2 (each covers 2 tiles) ALGORITHM (DP) 1 Define State dp[i][j] = min white tiles from pos i with j carpets 2 Transition Option A: Skip tile i Option B: Place carpet at i 3 Recurrence dp[i][j] = min( dp[i+1][j] + floor[i], dp[i+len][j-1]) 4 Compute Table Fill from right to left Sample DP values: i\j 0 1 2 0 3 1 0 2 2 0 0 4 0 0 0 FINAL RESULT Optimal Carpet Placement: 1 0 1 1 0 Carpet 1 Or another valid placement: Carpet White tiles visible: 2 Output: 2 Index 0 remains visible Key Insight: The DP approach considers two choices at each position: either skip the tile (adding its value if white) or place a carpet starting here. Since carpets have fixed length, placing one at position i covers tiles i to i+carpetLen-1, jumping the state to dp[i+carpetLen][j-1]. Time: O(n * numCarpets) TutorialsPoint - Minimum White Tiles After Covering With Carpets | Dynamic Programming Approach
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
12.5K Views
Medium Frequency
~35 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen