Skip to content

Commit dc6baf7

Browse files
committed
Runtime: 3555 ms (Top 46.27%) | Memory: 66.7 MB (Top 10.45%)
1 parent f875985 commit dc6baf7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
# Runtime: 3555 ms (Top 46.27%) | Memory: 66.7 MB (Top 10.45%)
2+
13
from sortedcontainers import SortedList
24

35
class Solution:
46
def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:
5-
rooms.sort(key=lambda x: x[1], reverse=True) # Sort by decreasing order of room size
6-
qArr = [[i, q] for i, q in enumerate(queries)] # Zip queries with their index
7-
qArr.sort(key=lambda x: x[1][1], reverse=True) # Sort by decreasing order of query minSize
7+
rooms.sort(key=lambda x: x[1], reverse=True) # Sort by decreasing order of room size
8+
qArr = [[i, q] for i, q in enumerate(queries)] # Zip queries with their index
9+
qArr.sort(key=lambda x: x[1][1], reverse=True) # Sort by decreasing order of query minSize
810

911
def searchClosestRoomId(preferredId):
10-
if len(roomIdsSoFar) == 0:
12+
if len(roomIdsSoFar) == 0:
1113
return -1
1214
cands = []
1315
i = roomIdsSoFar.bisect_right(preferredId)
14-
if i > 0:
16+
if i > 0:
1517
cands.append(roomIdsSoFar[i - 1])
16-
if i < len(roomIdsSoFar):
18+
if i < len(roomIdsSoFar):
1719
cands.append(roomIdsSoFar[i])
1820
return min(cands, key=lambda x: abs(x - preferredId))
1921

@@ -23,7 +25,7 @@ def searchClosestRoomId(preferredId):
2325
ans = [-1] * k
2426
for index, (prefferedId, minSize) in qArr:
2527
while i < n and rooms[i][1] >= minSize:
26-
roomIdsSoFar.add(rooms[i][0]) # Add id of the room which its size >= query minSize
28+
roomIdsSoFar.add(rooms[i][0]) # Add id of the room which its size >= query minSize
2729
i += 1
2830
ans[index] = searchClosestRoomId(prefferedId)
29-
return ans
31+
return ans

0 commit comments

Comments
 (0)