Skip to content

1353. Maximum Number of Events That Can Be Attended 👍

  • Time: $O(n\log n)$
  • Space: $O(n)$
 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
class Solution {
 public:
  int maxEvents(vector<vector<int>>& events) {
    int ans = 0;
    int d = 0;  // the current day
    int i = 0;  // events' index
    priority_queue<int, vector<int>, greater<>> minHeap;

    ranges::sort(events);

    while (!minHeap.empty() || i < events.size()) {
      // If no events are available to attend today, let time flies to the next
      // available event.
      if (minHeap.empty())
        d = events[i][0];
      // All the events starting from today are newly available.
      while (i < events.size() && events[i][0] == d)
        minHeap.push(events[i++][1]);
      // Greedily attend the event that'll end the earliest since it has higher
      // chance can't be attended in the future.
      minHeap.pop();
      ++ans;
      ++d;
      // Pop the events that can't be attended.
      while (!minHeap.empty() && minHeap.top() < d)
        minHeap.pop();
    }

    return ans;
  }
};
 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
class Solution {
  public int maxEvents(int[][] events) {
    int ans = 0;
    int d = 0; // the current day
    int i = 0; // events' index
    Queue<Integer> minHeap = new PriorityQueue<>();

    Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));

    while (!minHeap.isEmpty() || i < events.length) {
      // If no events are available to attend today, let time flies to the next
      // available event.
      if (minHeap.isEmpty())
        d = events[i][0];
      // All the events starting from today are newly available.
      while (i < events.length && events[i][0] == d)
        minHeap.offer(events[i++][1]);
      // Greedily attend the event that'll end the earliest since it has higher
      // chance can't be attended in the future.
      minHeap.poll();
      ++ans;
      ++d;
      // Pop the events that can't be attended.
      while (!minHeap.isEmpty() && minHeap.peek() < d)
        minHeap.poll();
    }

    return ans;
  }
}
 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
class Solution:
  def maxEvents(self, events: list[list[int]]) -> int:
    ans = 0
    minHeap = []
    i = 0  # events' index

    events.sort(key=lambda x: x[0])

    while minHeap or i < len(events):
      # If no events are available to attend today, let time flies to the next
      # available event.
      if not minHeap:
        d = events[i][0]
      # All the events starting from today are newly available.
      while i < len(events) and events[i][0] == d:
        heapq.heappush(minHeap, events[i][1])
        i += 1
      # Greedily attend the event that'll end the earliest since it has higher
      # chance can't be attended in the future.
      heapq.heappop(minHeap)
      ans += 1
      d += 1
      # Pop the events that can't be attended.
      while minHeap and minHeap[0] < d:
        heapq.heappop(minHeap)

    return ans