-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-building-where-alice-and-bob-can-meet.py
127 lines (115 loc) · 4.16 KB
/
find-building-where-alice-and-bob-can-meet.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Time: O(n + qlogn)
# Space: O(n)
# online solution, segment tree, binary search
class Solution(object):
def leftmostBuildingQueries(self, heights, queries):
"""
:type heights: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
# Range Maximum Query
class SegmentTree(object):
def __init__(self, N,
build_fn=lambda _: None,
query_fn=lambda x, y: max(x, y)):
self.tree = [None]*(2*2**((N-1).bit_length()))
self.build_fn = build_fn
self.query_fn = query_fn
self.build(0, N-1, 1)
def build(self, left, right, idx):
if left == right:
self.tree[idx] = self.build_fn(left)
return
mid = left + (right-left)//2
self.build(left, mid, idx*2)
self.build(mid+1, right, idx*2+1)
self.tree[idx] = self.query_fn(self.tree[idx*2], self.tree[idx*2+1])
def binary_search(self, L, R, left, right, idx, h):
if right < L or left > R:
return -1
if L <= left and right <= R:
if not self.tree[idx] > h:
return -1
if left == right:
return left
mid = left + (right-left)//2
i = self.binary_search(L, R, left, mid, idx*2, h)
return i if i != -1 else self.binary_search(L, R, mid+1, right, idx*2+1, h)
def build(i):
return heights[i]
result = [-1]*len(queries)
st = SegmentTree(len(heights), build_fn=build)
for i, (a, b) in enumerate(queries):
if a > b:
a, b = b, a
if a == b or heights[a] < heights[b]:
result[i] = b
continue
result[i] = st.binary_search(b+1, len(heights)-1, 0, len(heights)-1, 1, heights[a])
return result
# Time: O(n + qlogq)
# Space: O(n + q)
import heapq
# offline solution, heap
class Solution2(object):
def leftmostBuildingQueries(self, heights, queries):
"""
:type heights: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
result = [-1]*len(queries)
qs = [[] for _ in xrange(len(heights))]
for i, (a, b) in enumerate(queries):
if a > b:
a, b = b, a
if a == b or heights[a] < heights[b]:
result[i] = b
else:
qs[b].append((heights[a], i))
min_heap = []
for i, h in enumerate(heights):
for q in qs[i]:
heapq.heappush(min_heap, q)
while min_heap and min_heap[0][0] < h:
_, j = heapq.heappop(min_heap)
result[j] = i
return result
# Time: O(n + qlogn)
# Space: O(n + q)
# offline solution, mono stack, binary search
class Solution3(object):
def leftmostBuildingQueries(self, heights, queries):
"""
:type heights: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
def binary_search_right(left, right, check):
while left <= right:
mid = left + (right-left)//2
if not check(mid):
right = mid-1
else:
left = mid+1
return right
result = [-1]*len(queries)
qs = [[] for _ in xrange(len(heights))]
for i, (a, b) in enumerate(queries):
if a > b:
a, b = b, a
if a == b or heights[a] < heights[b]:
result[i] = b
else:
qs[b].append((heights[a], i))
stk = []
for b in reversed(xrange(len(heights))):
while stk and stk[-1][0] <= heights[b]:
stk.pop()
stk.append((heights[b], b))
for ha, i in qs[b]:
j = binary_search_right(0, len(stk)-1, lambda x: stk[x][0] > ha)
if j >= 0:
result[i] = stk[j][1]
return result