-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet18.java
More file actions
21 lines (21 loc) · 927 Bytes
/
leet18.java
File metadata and controls
21 lines (21 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int smallestChair(int[][] times, int targetFriend) {
int n = times.length;
Integer[] order = new Integer[n];
for (int i = 0; i < n; i++) order[i] = i;
Arrays.sort(order, (a, b) -> Integer.compare(times[a][0], times[b][0]));
PriorityQueue<Integer> emptySeats = new PriorityQueue<>();
PriorityQueue<int[]> takenSeats = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
for (int i = 0; i < n; i++) emptySeats.offer(i);
for (int i : order) {
int arrival = times[i][0], leave = times[i][1];
while (!takenSeats.isEmpty() && takenSeats.peek()[0] <= arrival) {
emptySeats.offer(takenSeats.poll()[1]);
}
int seat = emptySeats.poll();
if (i == targetFriend) return seat;
takenSeats.offer(new int[]{leave, seat});
}
return -1;
}
}