-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximize-area-of-square-hole-in-grid.py
55 lines (50 loc) · 1.51 KB
/
maximize-area-of-square-hole-in-grid.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Time: O(h + v), h = len(hBars), v = len(vBars)
# Space: O(h + v)
# array, hash table
class Solution(object):
def maximizeSquareHoleArea(self, n, m, hBars, vBars):
"""
:type n: int
:type m: int
:type hBars: List[int]
:type vBars: List[int]
:rtype: int
"""
def max_gap(arr):
result = l = 1
lookup = set(arr)
while lookup:
x = next(iter(lookup))
left = x
while left-1 in lookup:
left -= 1
right = x
while right+1 in lookup:
right += 1
for i in xrange(left, right+1):
lookup.remove(i)
result = max(result, (right-left+1)+1)
return result
return min(max_gap(hBars), max_gap(vBars))**2
# Time: O(hlogh + vlogv), h = len(hBars), v = len(vBars)
# Space: O(1)
# array, sort
class Solution2(object):
def maximizeSquareHoleArea(self, n, m, hBars, vBars):
"""
:type n: int
:type m: int
:type hBars: List[int]
:type vBars: List[int]
:rtype: int
"""
def max_gap(arr):
arr.sort()
result = l = 1
for i in xrange(len(arr)):
l += 1
result = max(result, l)
if i+1 != len(arr) and arr[i+1] != arr[i]+1:
l = 1
return result
return min(max_gap(hBars), max_gap(vBars))**2