Skip to content

Commit e20f7e8

Browse files
authored
Create maximum-white-tiles-covered-by-a-carpet.py
1 parent 5eb4a82 commit e20f7e8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(nlogn)
2+
# Space: O(1)
3+
4+
# sliding window
5+
class Solution(object):
6+
def maximumWhiteTiles(self, tiles, carpetLen):
7+
"""
8+
:type tiles: List[List[int]]
9+
:type carpetLen: int
10+
:rtype: int
11+
"""
12+
tiles.sort()
13+
result = left = gap = 0
14+
for right in xrange(len(tiles)):
15+
if right-1 >= 0:
16+
gap += (tiles[right][0]-1)-tiles[right-1][1]
17+
result = max(result, min(tiles[right][1]-tiles[left][0]+1, carpetLen)-gap)
18+
while tiles[right][1]-tiles[left][0]+1 > carpetLen:
19+
left += 1
20+
gap -= (tiles[left][0]-1)-tiles[left-1][1]
21+
result = max(result, min(tiles[right][1]-tiles[left][0]+1, carpetLen)-gap)
22+
return result

0 commit comments

Comments
 (0)