|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.utils.CommonUtils; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.PriorityQueue; |
| 8 | + |
| 9 | +/** |
| 10 | + * 1353. Maximum Number of Events That Can Be Attended |
| 11 | + * |
| 12 | + * Given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi. |
| 13 | + * You can attend an event i at any day d where startTimei <= d <= endTimei. Notice that you can only attend one event at any time d. |
| 14 | + * Return the maximum number of events you can attend. |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * Input: events = [[1,2],[2,3],[3,4]] |
| 18 | + * Output: 3 |
| 19 | + * Explanation: You can attend all the three events. |
| 20 | + * One way to attend them all is as shown. |
| 21 | + * Attend the first event on day 1. |
| 22 | + * Attend the second event on day 2. |
| 23 | + * Attend the third event on day 3. |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * Input: events= [[1,2],[2,3],[3,4],[1,2]] |
| 27 | + * Output: 4 |
| 28 | + * |
| 29 | + * Example 3: |
| 30 | + * Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]] |
| 31 | + * Output: 4 |
| 32 | + * |
| 33 | + * Example 4: |
| 34 | + * Input: events = [[1,100000]] |
| 35 | + * Output: 1 |
| 36 | + * |
| 37 | + * Example 5: |
| 38 | + * Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]] |
| 39 | + * Output: 7 |
| 40 | + * |
| 41 | + * Constraints: |
| 42 | + * 1 <= events.length <= 10^5 |
| 43 | + * events[i].length == 2 |
| 44 | + * 1 <= events[i][0] <= events[i][1] <= 10^5 |
| 45 | + * */ |
| 46 | +public class _1353 { |
| 47 | + public static class Solution1 { |
| 48 | + /** |
| 49 | + * Credit: https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/510263/JavaC%2B%2BPython-Priority-Queue |
| 50 | + * |
| 51 | + * 1. Sort events by start time, if ties, by end time; |
| 52 | + * 2. From day 1 to day 100,000, we add all events that start on this day into a priorityqueue, |
| 53 | + * also, we remove the events that closed on this day from the priorityqueue; |
| 54 | + * 3. attend the event that ends on this day (earliest, i.e. greedy) and pop it out of the priorityqueue |
| 55 | + * |
| 56 | + * */ |
| 57 | + public int maxEvents(int[][] events) { |
| 58 | + Arrays.sort(events, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]); |
| 59 | + PriorityQueue<Integer> heap = new PriorityQueue<>(); |
| 60 | + int maxEvents = 0; |
| 61 | + int i = 0; |
| 62 | + for (int day = 1; day <= 100000; day++) { |
| 63 | + while (i < events.length && events[i][0] == day) { |
| 64 | + heap.offer(events[i++][1]); |
| 65 | + } |
| 66 | + while (heap.size() > 0 && heap.peek() < day) { |
| 67 | + heap.poll(); |
| 68 | + } |
| 69 | + if (heap.size() > 0) { |
| 70 | + heap.poll(); |
| 71 | + maxEvents++; |
| 72 | + } |
| 73 | + } |
| 74 | + return maxEvents; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments