↓Skip to main content
  1. LeetCode/

LeetCode 3885: Design Event Manager

·2 mins· ·
LeetCode Medium Heap Design Hash-Map Lazy-Deletion
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-03-29

  • Submissions: Wrong Answer → Time Limit Exceeded → Accepted

Approach
#

This is nearly the same as LC 3815 Design Auction System and a textbook lazy-deletion problem:

  • priority[eventId]: the currently valid priority; polled events are removed from the dict.
  • A max-heap of (-priority, eventId): highest priority, then smallest id, at the top.
  • An update pushes a new entry and leaves the old one in the heap.
  • On a poll, if the top’s priority differs from the dict, or the event is no longer in the dict, it is stale: drop it and keep looking.

My Attempts
#

Wrong Answer: empty sets left in the dict
#

The first version used a priority -> set(eventId) dict. Updates removed the event from its old set, but the key was not deleted once the set became empty. Later, max(dict) could pick an empty set and return -1.

Time Limit Exceeded: max(dict) costs O(number of distinct priorities)
#

Deleting empty sets fixed the answers, but every pollHighest scanned all priorities with max(dict) and then called min(set), which timed out with many calls.

Solution
#

import heapq

class EventManager:

    def __init__(self, events: List[List[int]]):
        self.priority = {}
        self.heap = []
        for event_id, p in events:
            self.priority[event_id] = p
            self.heap.append((-p, event_id))
        heapq.heapify(self.heap)

    def updatePriority(self, eventId: int, newPriority: int) -> None:
        self.priority[eventId] = newPriority
        heapq.heappush(self.heap, (-newPriority, eventId))

    def pollHighest(self) -> int:
        while self.heap:
            neg_p, event_id = heapq.heappop(self.heap)
            if self.priority.get(event_id) == -neg_p:
                del self.priority[event_id]        # now taken
                return event_id
        return -1

heapify during initialization is O(n), faster than O(n log n) from pushing one by one.

Complexity
#

Each operation is amortized O(log n); space O(n + number of updates).

Mistakes to Avoid
#

  • Keeping a “priority → set” dict without deleting empty sets.
  • Calling max(dict) on every query.
  • Forgetting to delete from priority after a poll: another old entry for the same event could then look valid and be polled again.

Interview Explanation
#

A dict stores each event’s current priority, and a heap stores (-priority, id), so the top is the highest priority with the smallest id. Updates push a new entry and leave the old one. On a poll, if the top’s priority does not match the dict, or the event was already taken, I drop it and keep looking; a valid one is removed from the dict and returned. Each operation is amortized O(log n).

Related

LeetCode 3815: Design Auction System
·3 mins
LeetCode Medium Heap Design Hash-Map Lazy-Deletion
LeetCode 3815 note: one max-heap per item, push a new entry on every bid change, and drop stale entries on query
LeetCode 3829: Design Ride Sharing System
·2 mins
LeetCode Medium Queue Design Hash-Map Lazy-Deletion
LeetCode 3829 note: one queue each for riders and drivers; cancellations only update a set, and canceled riders at the front are dropped before matching
LeetCode 3607: Power Grid Maintenance
·3 mins
LeetCode Medium Heap Union-Find Graph Lazy-Deletion
LeetCode 3607 note: Union-Find groups, one min-heap per group, and lazy deletion for offline stations
LeetCode 3508: Implement Router
·3 mins
LeetCode Medium Design Binary-Search Queue Hash-Map
LeetCode 3508 note: a queue for FIFO, a set for duplicates, and an increasing time list per destination queried with bisect
LeetCode 1570: Dot Product of Two Sparse Vectors
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table Array
LeetCode 1570 note: store only non-zero (index, value) pairs, and compute the dot product by merging the two index-sorted lists with two pointers, multiplying only on equal indices
LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one