-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.java
49 lines (38 loc) · 1.66 KB
/
solution.java
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
import java.util.*;
class Solution {
public int smallestChair(int[][] times, int targetFriend) {
int n = times.length;
// List of arrivals with friend index
List<int[]> arrivals = new ArrayList<>();
for (int i = 0; i < n; i++) {
arrivals.add(new int[] { times[i][0], i });
}
// Sort friends by arrival time
arrivals.sort((a, b) -> Integer.compare(a[0], b[0]));
// Min-Heap to track available chairs
PriorityQueue<Integer> availableChairs = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
availableChairs.add(i);
}
// Priority queue to track when chairs are freed
PriorityQueue<int[]> leavingQueue = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
// Iterate through each friend based on arrival
for (int[] arrival : arrivals) {
int arrivalTime = arrival[0];
int friendIndex = arrival[1];
// Free chairs that are vacated before the current arrival time
while (!leavingQueue.isEmpty() && leavingQueue.peek()[0] <= arrivalTime) {
availableChairs.add(leavingQueue.poll()[1]);
}
// Assign the smallest available chair
int chair = availableChairs.poll();
// 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
leavingQueue.add(new int[] { times[friendIndex][1], chair });
}
return -1; // Should never reach here
}
}