1353. Maximum Number of Events That Can Be Attended

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
class Solution {
public int maxEvents(int[][] events) {
// 按开始时间升序排列,对于相同开始时间的会议,在一个批次里处理
Arrays.sort(events, Comparator.comparingInt(o -> o[0]));

// 小顶堆,存储
Queue<Integer> queue = new PriorityQueue<>();

int maxEvents = 0;
int currDay = 1;
int i = 0;
while (i < events.length || !queue.isEmpty()) {
// 将开始时间为今天的会议结束时间添加至堆
while (i < events.length && events[i][0] == currDay) {
queue.offer(events[i][1]);
i++;
}

// 移除掉今天不能参加的会议,即结束时间在今天之前的会议都不能参加
while (!queue.isEmpty() && queue.peek() < currDay) {
queue.poll();
}

if (!queue.isEmpty()) {
queue.poll(); // 选择一个结束时间最前的会议参加
maxEvents++;
}

currDay++;
}

return maxEvents;
}
}

References

1353. Maximum Number of Events That Can Be Attended