forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest-room.py
75 lines (66 loc) · 2.28 KB
/
closest-room.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
# Time: O(nlogn + klogk + klogn)
# Space: O(n + k)
from sortedcontainers import SortedList
class Solution(object):
def closestRoom(self, rooms, queries):
"""
:type rooms: List[List[int]]
:type queries: List[List[int]]
:rtype: List[int]
"""
def find_closest(ids, r):
result, min_dist = -1, float("inf")
i = ids.bisect_right(r)
if i-1 >= 0 and abs(ids[i-1]-r) < min_dist:
min_dist = abs(ids[i-1]-r)
result = ids[i-1]
if i < len(ids) and abs(ids[i]-r) < min_dist:
min_dist = abs(ids[i]-r)
result = ids[i]
return result
rooms.sort(key=lambda x: x[1], reverse=True)
for i, q in enumerate(queries):
q.append(i)
queries.sort(key=lambda x: x[1], reverse=True)
ids = SortedList()
i = 0
result = [-1]*len(queries)
for r, s, idx in queries:
while i < len(rooms) and rooms[i][1] >= s:
ids.add(rooms[i][0])
i += 1
result[idx] = find_closest(ids, r)
return result
# Time: O(nlogn + klogk + klogn)
# Space: O(n + k)
from sortedcontainers import SortedList
class Solution2(object):
def closestRoom(self, rooms, queries):
"""
:type rooms: List[List[int]]
:type queries: List[List[int]]
:rtype: List[int]
"""
def find_closest(ids, r):
result, min_dist = -1, float("inf")
i = ids.bisect_right(r)
if i-1 >= 0 and abs(ids[i-1]-r) < min_dist:
min_dist = abs(ids[i-1]-r)
result = ids[i-1]
if i < len(ids) and abs(ids[i]-r) < min_dist:
min_dist = abs(ids[i]-r)
result = ids[i]
return result
rooms.sort(key=lambda x: x[1])
for i, q in enumerate(queries):
q.append(i)
queries.sort(key=lambda x: x[1])
ids = SortedList(i for i, _ in rooms)
i = 0
result = [-1]*len(queries)
for r, s, idx in queries:
while i < len(rooms) and rooms[i][1] < s:
ids.remove(rooms[i][0])
i += 1
result[idx] = find_closest(ids, r)
return result