Minimum White Tiles After Covering With Carpets - Problem

Imagine you're a floor designer working on a project where you need to cover white tiles with black carpets to achieve a specific aesthetic. You have a floor represented by a binary string, where:

  • '0' represents a black tile (already perfect)
  • '1' represents a white tile (needs to be covered)

You have numCarpets black carpets available, each exactly carpetLen tiles long. Your goal is to strategically place these carpets to minimize the number of visible white tiles.

Key Rules:

  • Carpets can overlap each other
  • Each carpet covers carpetLen consecutive tiles
  • You want to minimize uncovered white tiles

Example: If floor = "10110", numCarpets = 1, carpetLen = 2, you could place the carpet at position 1 to cover tiles [1,2], leaving only 1 white tile visible (at position 4).

Input & Output

example_1.py โ€” Basic Case
$ Input: floor = "10110", numCarpets = 1, carpetLen = 2
โ€บ Output: 2
๐Ÿ’ก Note: We can place the carpet at position 1 covering tiles [1,2] ("01"), leaving white tiles at positions 0 and 4 visible, for a total of 2 white tiles.
example_2.py โ€” Multiple Carpets
$ Input: floor = "11111", numCarpets = 2, carpetLen = 3
โ€บ Output: 0
๐Ÿ’ก Note: We can place first carpet at position 0 covering [0,1,2] and second carpet at position 2 covering [2,3,4]. All white tiles are covered (carpets can overlap).
example_3.py โ€” No White Tiles
$ Input: floor = "00000", numCarpets = 3, carpetLen = 2
โ€บ Output: 0
๐Ÿ’ก Note: All tiles are already black, so no white tiles are visible regardless of carpet placement.

Visualization

Tap to expand
Carpet 110110Carpet covers 2 white tilesRemaining white tiles: 2Floor: "10110"Carpets: 1, Length: 2Result: 2 white tiles uncovered๐ŸŽฏ Key Insight: Use DP to find optimal carpet placement minimizing visible white tiles
Understanding the Visualization
1
Identify White Tiles
Scan the floor to find all white tiles (1s) that need covering
2
Evaluate Placements
For each position, consider placing a carpet to maximize white tile coverage
3
Make Optimal Choice
Use DP to choose between placing carpet here or trying next position
4
Minimize Remaining
Count white tiles that remain uncovered after optimal placement
Key Takeaway
๐ŸŽฏ Key Insight: Dynamic programming allows us to efficiently explore all placement strategies while avoiding redundant computations through memoization.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— k)

n positions ร— k carpets states, each computed once

n
2n
โœ“ Linear Growth
Space Complexity
O(n ร— k)

Memoization table stores results for all (position, carpets) pairs

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค floor.length โ‰ค 1000
  • floor[i] is either '0' or '1'
  • 0 โ‰ค numCarpets โ‰ค 1000
  • 1 โ‰ค carpetLen โ‰ค floor.length
  • Note: Carpets can overlap each other
Asked in
Google 42 Amazon 38 Meta 28 Microsoft 25
28.4K Views
Medium Frequency
~25 min Avg. Time
856 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