-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.py
36 lines (26 loc) · 1.33 KB
/
solution.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
import heapq
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
n = len(times)
# Create a list of arrivals with friend index
arrivals = [(times[i][0], i) for i in range(n)]
# Sort friends by arrival time
arrivals.sort()
# Min-Heap to track available chairs
availableChairs = list(range(n))
heapq.heapify(availableChairs)
# Priority queue to track when chairs are freed
leavingQueue = []
# Iterate through each friend based on arrival
for arrivalTime, friendIndex in arrivals:
# Free chairs that are vacated before the current arrival time
while leavingQueue and leavingQueue[0][0] <= arrivalTime:
heapq.heappush(availableChairs, heapq.heappop(leavingQueue)[1])
# Assign the smallest available chair
chair = heapq.heappop(availableChairs)
# If this is the target friend, return their chair number
if friendIndex == targetFriend:
return chair
# Mark the chair as being used until the friend's leave time
heapq.heappush(leavingQueue, (times[friendIndex][1], chair))
return -1 # Should never reach here