-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmaximum-white-tiles-covered-by-a-carpet.py
40 lines (30 loc) · 1.25 KB
/
maximum-white-tiles-covered-by-a-carpet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from typing import List
class Solution:
def maximumWhiteTiles(self, tiles: List[List[int]], carpet_len: int) -> int:
tiles.sort()
# Right side of the carpet
right = 0
# Tiles covered by cover completely
tiles_covered = 0
result = 0
for left in range(len(tiles)):
# Predicate: carpet should cover all the tiles completely all the time
# Right pointer after the loop might point to a tile that isn't
# covered by the carpet at all, or partially covered
while right < len(tiles) and tiles[right][1] < tiles[left][0] + carpet_len:
tiles_covered += tiles[right][1] - tiles[right][0] + 1
right += 1
# Add a tile that might be covered partially
additional = max(
0,
tiles[left][0] + carpet_len - tiles[right][0]
if right < len(tiles)
else 0,
)
result = max(result, tiles_covered + additional)
# Handle situation when right pointer didn't move
if left < right:
tiles_covered -= tiles[left][1] - tiles[left][0] + 1
else:
right = left + 1
return result