Untitled
unknown
plain_text
9 months ago
1.3 kB
5
Indexable
import java.util.*;
class Solution {
public int maxEvents(int[][] events) {
// Sort events by start day
Arrays.sort(events, (a, b) -> a[0] - b[0]);
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
int attended = 0;
int day = 0;
int i = 0;
int n = events.length;
// Iterate through days
while (i < n || !minHeap.isEmpty()) {
// Move to the next available day
if (minHeap.isEmpty()) {
day = events[i][0];
}
// Add all events that start on or before the current day
while (i < n && events[i][0] <= day) {
minHeap.offer(events[i][1]); // Add event's end day to the heap
i++;
}
// Remove events that have expired (end day < current day)
while (!minHeap.isEmpty() && minHeap.peek() < day) {
minHeap.poll();
}
// Attend the event that ends the earliest
if (!minHeap.isEmpty()) {
minHeap.poll(); // Attend the event
attended++;
day++; // Move to the next day
}
}
return attended;
}
}
Editor is loading...
Leave a Comment