-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-area-rectangle-with-point-constraints-i.py
66 lines (59 loc) · 2.25 KB
/
maximum-area-rectangle-with-point-constraints-i.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
56
57
58
59
60
61
62
63
64
65
66
# Time: O(nlogn)
# Space: O(n)
# sort, fenwick tree, hash table
class Solution(object):
def maxRectangleArea(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
class BIT(object): # 0-indexed.
def __init__(self, n):
self.__bit = [0]*(n+1) # Extra one for dummy node.
def add(self, i, val):
i += 1 # Extra one for dummy node.
while i < len(self.__bit):
self.__bit[i] += val
i += (i & -i)
def query(self, i):
i += 1 # Extra one for dummy node.
ret = 0
while i > 0:
ret += self.__bit[i]
i -= (i & -i)
return ret
points.sort()
y_to_idx = {y:idx for idx, y in enumerate(sorted(set(y for _, y in points)))}
bit = BIT(len(y_to_idx))
lookup = {}
result = -1
for i, (x, y) in enumerate(points):
y_idx = y_to_idx[y]
bit.add(y_idx, +1)
if not (i-1 >= 0 and points[i-1][0] == x):
continue
prev_y_idx = y_to_idx[points[i-1][1]]
curr = bit.query(y_idx)-bit.query(prev_y_idx-1)
if (prev_y_idx, y_idx) in lookup and lookup[prev_y_idx, y_idx][0] == curr-2:
result = max(result, (x-lookup[prev_y_idx, y_idx][1])*(y-points[i-1][1]))
lookup[prev_y_idx, y_idx] = (curr, x)
return result
# Time: O(n^2)
# Space: O(1)
# sort, brute force
class Solution2(object):
def maxRectangleArea(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
result = -1
points.sort()
for i in xrange(len(points)-3):
if points[i][0] != points[i+1][0]:
continue
j = next((j for j in xrange(i+2, len(points)-1) if points[i][1] <= points[j][1] <= points[i+1][1]), len(points)-1)
if j == len(points)-1 or not (points[j][0] == points[j+1][0] and points[i][1] == points[j][1] and points[i+1][1] == points[j+1][1]):
continue
result = max(result, (points[i+1][1]-points[i][1])*(points[j][0]-points[i][0]))
return result