-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosestRoom.py
More file actions
33 lines (31 loc) · 1 KB
/
closestRoom.py
File metadata and controls
33 lines (31 loc) · 1 KB
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/closest-room/
# Author: Miao Zhang
# Date: 2021-06-11
from sortedcontainers import SortedSet
class Solution:
def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:
n = len(rooms)
m = len(queries)
for i in range(m):
queries[i].append(i)
queries.sort(key=lambda x: -x[1])
rooms.sort(key=lambda x: -x[1])
res = [-1] * m
j = 0
ids = SortedSet()
for i, size, idx in queries:
while j < n and rooms[j][1] >= size:
ids.add(rooms[j][0])
j += 1
if not ids: continue
idx1 = bisect.bisect_left(ids, i)
i1 = float('inf')
if idx1 != len(ids):
i1 = ids[idx1]
i2 = i1
if idx1 != 0:
i2 = ids[idx1 - 1]
res[idx] = i1 if abs(i1 - i) < abs(i2 - i) else i2
return res