2271. Maximum White Tiles Covered by a Carpet

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2271

class Solution:
    def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
        tiles.sort()
        n = len(tiles)
        s = res = j = 0
        for i, (li, ri) in enumerate(tiles):
            while j < n and tiles[j][1] - li + 1 <= carpetLen:
                s += tiles[j][1] - tiles[j][0] + 1
                j += 1
            if j < n and li + carpetLen > tiles[j][0]:
                res = max(res, s + li + carpetLen - tiles[j][0])
            else:
                res = max(res, s)
            s -= ri - li + 1
        return res