3885. Design Event Manager
Description
You are given an initial list of events, where each event has a unique eventId and a priority.
Implement the EventManager class:
EventManager(int[][] events)Initializes the manager with the given events, whereevents[i] = [eventIdi, priorityi].void updatePriority(int eventId, int newPriority)Updates the priority of the active event with ideventIdtonewPriority.int pollHighest()Removes and returns theeventIdof the active event with the highest priority. If multiple active events have the same priority, return the smallesteventIdamong them. If there are no active events, return -1.
An event is called active if it has not been removed by pollHighest().
Example 1:
Input:
["EventManager", "pollHighest", "updatePriority", "pollHighest", "pollHighest"]
[[[[5, 7], [2, 7], [9, 4]]], [], [9, 7], [], []]
Output:
[null, 2, null, 5, 9]
Explanation
EventManager eventManager = new EventManager([[5,7], [2,7], [9,4]]); // Initializes the manager with three eventseventManager.pollHighest(); // both events 5 and 2 have priority 7, so return the smaller id 2
eventManager.updatePriority(9, 7); // event 9 now has priority 7
eventManager.pollHighest(); // remaining highest priority events are 5 and 9, return 5
eventManager.pollHighest(); // return 9
Example 2:
Input:
["EventManager", "pollHighest", "pollHighest", "pollHighest"]
[[[[4, 1], [7, 2]]], [], [], []]
Output:
[null, 7, 4, -1]
Explanation
EventManager eventManager = new EventManager([[4,1], [7,2]]); // Initializes the manager with two eventseventManager.pollHighest(); // return 7
eventManager.pollHighest(); // return 4
eventManager.pollHighest(); // no events remain, return -1
Constraints:
1 <= events.length <= 105events[i] = [eventId, priority]1 <= eventId <= 1091 <= priority <= 109- All the values of
eventIdineventsare unique. 1 <= newPriority <= 109- For every call to
updatePriority,eventIdrefers to an active event. - At most
105calls in total will be made toupdatePriorityandpollHighest.
Solutions
Solution 1: Sorted Set
Thinking
Poll the highest-priority live event, breaking ties by smallest \(\textit{eventId}\), and support priority updates. Up to \(10^5\) operations.
The max is an ordered-set endpoint; an update must erase the old key and insert the new one.
Store \((-\textit{priority},\textit{eventId})\) so higher priority and smaller id come first, and a hash map remembers the current priority for deletion.
A poll also drops the hash entry.
We define a sorted set \(\textit{sl}\) to store tuples of priority and id \((-\textit{priority}, \textit{eventId})\) for all active events, and a hash map \(\textit{d}\) to store the priority of each event.
During initialization, we iterate over the given event list, add the tuple of priority and id for each event into the sorted set \(\textit{sl}\), and store each event's priority in the hash map \(\textit{d}\).
For the \(\textit{updatePriority}(eventId, newPriority)\) operation, we first retrieve the old priority of the event from the hash map \(\textit{d}\), then remove the tuple of the old priority and event id from the sorted set \(\textit{sl}\), add the tuple of the new priority and event id into \(\textit{sl}\), and update the event's priority in \(\textit{d}\).
For the \(\textit{pollHighest}()\) operation, we first check whether the sorted set \(\textit{sl}\) is empty. If it is, return -1. Otherwise, we retrieve the event with the highest priority (i.e., the first element) from \(\textit{sl}\), remove its tuple, delete the event's priority information from \(\textit{d}\), and return the event's id.
In terms of time complexity, initialization takes \(O(n \log n)\) time, where \(n\) is the number of initial events. Each call to \(\textit{updatePriority}\) and \(\textit{pollHighest}\) takes \(O(\log n)\) time. The space complexity is \(O(n)\), where \(n\) is the number of active events.
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 | |
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 35 36 37 38 39 40 41 | |
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 35 36 37 38 | |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |