-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0497.py
30 lines (22 loc) · 1.11 KB
/
LC0497.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
import heapq
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
openChairs, timeHeap = [], []
output, startTime = 0, 0
# populate minHeap with all posible chairs open
for i in range(len(times)):
if i == targetFriend:
startTime = times[i][0]
heapq.heappush(openChairs, i)
times.sort(key=lambda x : x[0]) # sort by arrival time
for i, timeObj in enumerate(times):
arrival, leaving = timeObj
# if our new arrival time is greater than any previous leaving time,
# pop our timeHeap and push occupied chair # to openChairs
while len(timeHeap) and arrival >= timeHeap[0][0]:
oldTime, chair = heapq.heappop(timeHeap)
heapq.heappush(openChairs, chair)
if startTime == arrival:
return heapq.heappop(openChairs) # return next open chair
heapq.heappush(timeHeap, (leaving, heapq.heappop(openChairs)))
return -1