Problem solution · Python

Design Event Manager

Design Event Manager: a Python solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Heap or priority queue
Source
Kamyu LeetCode Solutions
Length
96 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Heap or priority queue

For Design Event Manager, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. Discard stale entries when necessary and process the best live candidate.

Code notes

  • 96 lines of Python from the credited upstream file design-event-manager.py.
  • The implementation visibly relies on sequence storage, work queue.
  • No explicit loop blocks detected.

Complexity

Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeDesign Event Manager · PythonPython
Use this to learn the idea, then write your own version.
# Time:  ctor:           O(n)#        updatePriority: O(logn), amortized#        pollHightest:   O(logn), amortized# Space: O(n) import heapq  # heap, hash tableclass EventManager(object):     def __init__(self, events):        """        :type events: List[List[int]]        """        self.__lookup = {}        self.__max_heap = []        for i, p in events:            self.__lookup[i] = p            self.__max_heap.append((-p, i))        heapq.heapify(self.__max_heap)     def updatePriority(self, eventId, newPriority):        """        :type eventId: int        :type newPriority: int        :rtype: None        """        self.__lookup[eventId] = newPriority        heapq.heappush(self.__max_heap, (-newPriority, eventId))        if len(self.__max_heap) > 2*len(self.__lookup):            self.__rebuild()            def pollHighest(self):        """        :rtype: int        """        while self.__max_heap:            p, i = heapq.heappop(self.__max_heap)            if i in self.__lookup and self.__lookup[i] == -p:                del self.__lookup[i]                return i        return -1     def __rebuild(self):        lookup = {}        max_heap = []        while self.__max_heap:            p, i = heapq.heappop(self.__max_heap)            if i in self.__lookup and self.__lookup[i] == -p:                del self.__lookup[i]                lookup[i] = -p                heapq.heappush(max_heap, (p, i))        self.__lookup, self.__max_heap = lookup, max_heap  # Time:  ctor:           O(nlogn)#        updatePriority: O(logn)#        pollHightest:   O(logn)# Space: O(n)from sortedcontainers import SortedList  # sorted list, hash tableclass EventManager2(object):     def __init__(self, events):        """        :type events: List[List[int]]        """        self.__lookup = {}        self.__sl = SortedList()        for i, p in events:            self.__lookup[i] = p            self.__sl.add((p, -i))     def updatePriority(self, eventId, newPriority):        """        :type eventId: int        :type newPriority: int        :rtype: None        """        self.__sl.remove((self.__lookup[eventId], -eventId))        self.__lookup[eventId] = newPriority        self.__sl.add((newPriority, -eventId))       def pollHighest(self):        """        :rtype: int        """        if not self.__sl:            return -1        result = -self.__sl.pop()[1]        self.__lookup.pop(result)        return result 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗