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;  // current day
    int i = 0;  // events' index
    priority_queue<int, vector<int>, greater<>> minHeap;

    sort(begin(events), end(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 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
      // because it has higher chance can't be attended in the future
      minHeap.pop();
      ++ans;
      ++d;
      // pop 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; // current day
    int i = 0; // events' index
    Queue<Integer> minHeap = new PriorityQueue<>();

    Arrays.sort(events, (a, b) -> 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 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
      // because it has higher chance can't be attended in the future
      minHeap.poll();
      ++ans;
      ++d;
      // pop 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 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
      # because it has higher chance can't be attended in the future
      heapq.heappop(minHeap)
      ans += 1
      d += 1
      # pop events that can't be attended
      while minHeap and minHeap[0] < d:
        heapq.heappop(minHeap)

    return ans