-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
50 lines (36 loc) · 1.26 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
50
class Solution {
public int smallestChair(int[][] times, int targetFriend) {
List<int[]> events = new ArrayList<>();
for (int i = 0; i < times.length; i++) {
events.add(new int[] { times[i][0], i, 1 });
events.add(new int[] { times[i][1], i, -1 });
}
Collections.sort(events, (a, b) -> {
if (a[0] != b[0]) {
return Integer.compare(a[0], b[0]);
} else {
return Integer.compare(a[2], b[2]);
}
});
PriorityQueue<Integer> availableChairs = new PriorityQueue<>();
int[] chairs = new int[times.length];
int nextChair = 0;
for (int[] event : events) {
int time = event[0];
int friendIndex = event[1];
int type = event[2];
if (type == 1) {
if (availableChairs.isEmpty()) {
chairs[friendIndex] = nextChair++;
} else {
chairs[friendIndex] = availableChairs.poll();
}
if (friendIndex == targetFriend) {
}
} else {
availableChairs.offer(chairs[friendIndex]);
}
}
return chairs[targetFriend];
}
}