Maximum White Tiles Covered by a Carpet - Problem
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.
You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.
Return the maximum number of white tiles that can be covered by the carpet.
Input & Output
Example 1 — Basic Case
$
Input:
tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
›
Output:
9
💡 Note:
Place carpet from position 10 to 19. It covers tiles [10,11] (2 tiles), [12,18] (7 tiles), totaling 9 tiles.
Example 2 — Optimal Positioning
$
Input:
tiles = [[10,11],[1,1]], carpetLen = 2
›
Output:
2
💡 Note:
Place carpet from position 10 to 11, covering both tiles [10,11] completely for 2 tiles total.
Example 3 — Single Large Range
$
Input:
tiles = [[1,100]], carpetLen = 10
›
Output:
10
💡 Note:
The carpet can cover at most 10 consecutive positions from the range [1,100].
Constraints
- 1 ≤ tiles.length ≤ 5 × 104
- tiles[i].length == 2
- 1 ≤ li ≤ ri ≤ 109
- 1 ≤ carpetLen ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Tile ranges [[1,5],[10,11],[12,18]] and carpet length 10
2
Process
Find optimal carpet position to maximize tile coverage
3
Output
Maximum number of white tiles that can be covered: 9
Key Takeaway
🎯 Key Insight: Sort tiles by position and use sliding window to efficiently find the optimal carpet placement that maximizes white tile coverage.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code