↓Skip to main content
  1. LeetCode/

LeetCode 2271: Maximum White Tiles Covered by a Carpet

·2 mins· ·
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-11

  • Submissions: Accepted ×2 (written on 2026-01-11 and 01-14)

Approach
#

The carpet can always start at some segment’s start: if its left end is on a gap or in the middle of a segment, pushing it right to the next start loses only gaps or tiles already counted on the left and can only gain on the right.

So sort the segments by start and try the carpet at each tiles[left][0], ending at start + carpetLen - 1:

  • right absorbs every segment that is fully covered (tiles[right][1] <= end), summing their lengths.
  • The next segment tiles[right] may be partially covered: max(0, end - tiles[right][0] + 1).
  • Before moving to the next left, subtract the length of tiles[left].

The carpet’s end moves right with left, so right never moves back.

My Solution
#

class Solution:
    def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
        tiles.sort()
        n = len(tiles)
        right = covered = best = 0                 # covered: total length of fully covered segments
        for left in range(n):
            end = tiles[left][0] + carpetLen - 1
            while right < n and tiles[right][1] <= end:
                covered += tiles[right][1] - tiles[right][0] + 1
                right += 1
            partial = max(0, end - tiles[right][0] + 1) if right < n else 0
            best = max(best, covered + partial)
            if right > left:
                covered -= tiles[left][1] - tiles[left][0] + 1
            else:
                right = left + 1                   # this segment itself was not fully covered
        return best

My original had no else branch: when the carpet cannot even cover tiles[left], right == left and subtracting its length makes covered negative. The original still passed, because the next round corrects the value; an explicit branch is easier to read.

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • Trying every possible carpet position: coordinates reach 10^9.
  • Forgetting that the last segment may be only partly covered.

Interview Explanation
#

Starting the carpet at some segment’s start is never worse, so I sort the segments and try each start. A second pointer sums the fully covered segments, and the next one may be partly covered, counted separately. When the left pointer moves on I subtract the segment that left, and the right pointer never moves back. Sorting dominates: O(n log n).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost
LeetCode 2024: Maximize the Confusion of an Exam
·2 mins
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 2024 note: a window is valid when the rarer of T and F appears at most k times (flip all of them); shrink the left when it is not