Design Event Manager Problem
#3885
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3885.
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] = [eventId_i, priority_i].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 events
eventManager.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 events
eventManager.pollHighest(); // return 7
eventManager.pollHighest(); // return 4
eventManager.pollHighest(); // no events remain, return -1
Constraints:
1 <= events.length <= 10^5events[i] = [eventId, priority]1 <= eventId <= 10^91 <= priority <= 10^9- All the values of
eventIdineventsare unique. 1 <= newPriority <= 10^9- For every call to
updatePriority,eventIdrefers to an active event. - At most
10^5calls in total will be made toupdatePriorityandpollHighest.
Solution
Approach: Heap with Lazy Deletion
Keep a hash map d from each active event id to its current priority, and a heap ordered by highest priority first, then smallest id.
- Initialization: put every event in
dand in the heap. updatePriority(eventId, newPriority): setd[eventId] = newPriorityand push the new pair into the heap. The old pair stays in the heap but is now out of date.pollHighest(): pop pairs until one matches the current priority ind, meaning it is not stale. Remove that event fromdand return its id. If the heap runs empty, return-1.
Each pushed pair is popped at most once, so the work is amortised.
Complexity: initialization O(n); updatePriority and pollHighest take O(log n) amortised, where n is the number of pushed pairs. Space O(n).
import heapq
class EventManager:
def __init__(self, events: List[List[int]]):
self.d = {}
self.h = []
for eventId, priority in events:
self.d[eventId] = priority
self.h.append((-priority, eventId))
heapq.heapify(self.h)
def updatePriority(self, eventId: int, newPriority: int) -> None:
self.d[eventId] = newPriority
heapq.heappush(self.h, (-newPriority, eventId))
def pollHighest(self) -> int:
while self.h:
p, eventId = heapq.heappop(self.h)
if self.d.get(eventId) == -p:
del self.d[eventId]
return eventId
return -1
# Your EventManager object will be instantiated and called as such:
# obj = EventManager(events)
# obj.updatePriority(eventId,newPriority)
# param_2 = obj.pollHighest()