Problem solution · C++

Design Event Manager

Design Event Manager: a C++ 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
93 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

  • 93 lines of C++ from the credited upstream file design-event-manager.cpp.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
  • 4 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 · C++C++
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) // heap, hash tableclass EventManager {public:    EventManager(vector<vector<int>>& events) {        vector<pair<int, int>> pairs;        for (const auto& e : events) {            lookup_[e[0]] = e[1];            pairs.emplace_back(e[1], -e[0]);        }        priority_queue<pair<int, int>> tmp(cbegin(pairs), cend(pairs));        max_heap_.swap(tmp);    }        void updatePriority(int eventId, int newPriority) {        lookup_[eventId] = newPriority;        max_heap_.emplace(newPriority, -eventId);        if (size(max_heap_) > 2 * size(lookup_)) {            rebuild();        }    }        int pollHighest() {        for (; !empty(max_heap_); max_heap_.pop()) {            const auto [p, i] = max_heap_.top();            if (lookup_.count(-i) && lookup_[-i] == p) {                lookup_.erase(-i);                return -i;            }        }        return -1;    } private:    void rebuild() {         unordered_map<int, int> lookup;         priority_queue<pair<int, int>> max_heap;         for (; !empty(max_heap_); max_heap_.pop()) {            const auto [p, i] = max_heap_.top();            if (lookup_.count(-i) && lookup_[-i] == p) {                lookup_.erase(-i);                lookup[-i] = p;                max_heap.emplace(p, i);            }        }        lookup_.swap(lookup);        max_heap_.swap(max_heap);    }     unordered_map<int, int> lookup_;    priority_queue<pair<int, int>> max_heap_;}; // Time:  ctor:           O(nlogn)//        updatePriority: O(logn)//        pollHightest:   O(logn)// Space: O(n)// bst, hash tableclass EventManager2 {public:    EventManager2(vector<vector<int>>& events) {        for (const auto& e : events) {            lookup_[e[0]] = e[1];            bst_.emplace(e[1], -e[0]);        }    }        void updatePriority(int eventId, int newPriority) {        bst_.erase(pair(lookup_[eventId], -eventId));        lookup_[eventId] = newPriority;        bst_.emplace(newPriority, -eventId);    }        int pollHighest() {        if (empty(bst_)) {            return -1;        }        auto it = prev(cend(bst_));        const int result = -it->second;        bst_.erase(it);        lookup_.erase(result);        return result;    } private:    unordered_map<int, int> lookup_;    set<pair<int, int>> bst_;}; 

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 ↗