Maximum White Tiles Covered by a Carpet - Problem

Imagine you're an interior designer with a special carpet that you want to place optimally to cover as many white tiles as possible on a floor.

You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li โ‰ค j โ‰ค ri is colored white. Think of this as continuous segments of white tiles on a number line.

You also have a carpet of fixed length carpetLen that can be placed anywhere on this number line.

Goal: Find the optimal position to place your carpet so that it covers the maximum number of white tiles.

Example: If you have white tile segments [[1,5], [10,11], [12,18]] and a carpet of length 4, you need to find where to place this carpet to cover the most white tiles possible.

Input & Output

example_1.py โ€” Basic Coverage
$ Input: tiles = [[1,5],[10,11],[12,18]], carpetLen = 4
โ€บ Output: 4
๐Ÿ’ก Note: Place the carpet starting at position 10. It will cover tiles 10,11 (2 tiles) and tiles 12,13 (2 tiles) for a total of 4 white tiles.
example_2.py โ€” Optimal Positioning
$ Input: tiles = [[10,11],[1,1]], carpetLen = 2
โ€บ Output: 2
๐Ÿ’ก Note: Place the carpet at position 10. It covers tiles 10,11 which gives us 2 white tiles. This is better than covering tile 1 (only 1 tile).
example_3.py โ€” Single Large Segment
$ Input: tiles = [[1,10]], carpetLen = 3
โ€บ Output: 3
๐Ÿ’ก Note: Place the carpet anywhere within the segment [1,10]. Since the carpet length is 3, it will always cover exactly 3 white tiles from this segment.

Constraints

  • 1 โ‰ค tiles.length โ‰ค 5 ร— 104
  • tiles[i].length == 2
  • 1 โ‰ค li โ‰ค ri โ‰ค 109
  • 1 โ‰ค carpetLen โ‰ค 109
  • No two tiles overlap (tiles represent disjoint segments)

Visualization

Tap to expand
White Tile Segments on Number Line[1,8][12,15][18,27][30,35]Carpet Length = 8Optimal Carpet PositionStart: 12End: 19Coverage Analysis๐ŸŽฏ Key Insight: Only try carpet positions at segment starts๐Ÿ“Š Coverage at position 12: [12,15] (4 tiles) + [18,19] (2 tiles) = 6 tilesโšก Time Complexity: O(n log n) with sorting + sliding window๐Ÿ’พ Space Complexity: O(1) additional space needed๐Ÿ† Optimal Result: Maximum 6 white tiles can be covered
Understanding the Visualization
1
Identify Segments
Map out all white tile segments on the number line
2
Sort by Position
Arrange segments in left-to-right order for efficient processing
3
Try Key Positions
Only test carpet positions starting at segment boundaries
4
Calculate Coverage
For each position, efficiently count total covered tiles
5
Find Maximum
Return the position that gives maximum coverage
Key Takeaway
๐ŸŽฏ Key Insight: The optimal carpet position will always start at a tile segment boundary. By sorting segments and using a sliding window approach, we can efficiently find the maximum coverage in O(n log n) time.
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 31
43.2K Views
Medium-High Frequency
~18 min Avg. Time
1.8K 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