Maximum White Tiles Covered by a Carpet LeetCode solution in Python

1
class Solution:
2
def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
3
tiles.sort()
4
# j: window index
5
j = cover = res = 0
6
for i in range(len(tiles)):
7
# slide the window as far as we can to cover fully the intervals with the carpet
8
while j < len(tiles) and tiles[j][1] - tiles[i][0] + 1 <= carpetLen:
9
cover += tiles[j][1] - tiles[j][0] + 1
10
j += 1
11
# process the remnant, that is, when the tiles[j] is covered by the carpet partially(not fully)
12
if j < len(tiles) and tiles[j][0] - tiles[i][0] + 1 <= carpetLen:
13
res = max(res, cover + carpetLen - (tiles[j][0] - tiles[i][0]))
14
else:
15
res = max(res, cover)
16
# when the tiles[j] is covered partially, the interval is not added to the variable cover
17
if i != j:
18
cover -= tiles[i][1] - tiles[i][0] + 1
19
j = max(j, i + 1)
20
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0