Problem solution · C++

Design Auction System

Design Auction System: 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
95 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 Auction System, 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

  • 95 lines of C++ from the credited upstream file design-auction-system.cpp.
  • The implementation visibly relies on hash lookup, ordered lookup, work queue.
  • 1 loop block 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 Auction System · C++C++
Use this to learn the idea, then write your own version.
// Time:  ctor:             O(1)//        addBid:           O(logn)//        updateBid:        O(logn)//        removeBid:        O(logn)//        getHighestBidder: O(1), amortized// Space: O(n) // hash table, heapclass AuctionSystem {public:    AuctionSystem() {            }        void addBid(int userId, int itemId, int bidAmount) {        bids_[itemId][userId] = bidAmount;        bidders_[itemId].emplace(bidAmount, userId);    }        void updateBid(int userId, int itemId, int newAmount) {        addBid(userId, itemId, newAmount);    }        void removeBid(int userId, int itemId) {        bids_[itemId].erase(userId);        if (empty(bids_[itemId])) {            bids_.erase(itemId);        }    }        int getHighestBidder(int itemId) {        if (!bidders_.count(itemId)) {            return -1;        }        while (!empty(bidders_[itemId])) {            const auto [p, u] = bidders_[itemId].top();            if (bids_[itemId][u] == p) {                return u;            }            bidders_[itemId].pop();        }        bidders_.erase(itemId);        return -1;    } private:    unordered_map<int, unordered_map<int, int>> bids_;    unordered_map<int, priority_queue<pair<int, int>>> bidders_;}; // Time:  ctor:             O(1)//        addBid:           O(logn)//        updateBid:        O(logn)//        removeBid:        O(logn)//        getHighestBidder: O(1)// Space: O(n)// hash table, bstclass AuctionSystem2 {public:    AuctionSystem2() {            }        void addBid(int userId, int itemId, int bidAmount) {        if (bids_[itemId].count(userId)) {            bidders_[itemId].erase({bids_[itemId][userId], userId});        }        bids_[itemId][userId] = bidAmount;        bidders_[itemId].emplace(bidAmount, userId);    }        void updateBid(int userId, int itemId, int newAmount) {        addBid(userId, itemId, newAmount);    }        void removeBid(int userId, int itemId) {        bidders_[itemId].erase({bids_[itemId][userId], userId});        if (empty(bidders_[itemId])) {            bidders_.erase(itemId);        }        bids_[itemId].erase(userId);        if (empty(bids_[itemId])) {            bids_.erase(itemId);        }    }        int getHighestBidder(int itemId) {        return bidders_.count(itemId) ? rbegin(bidders_[itemId])->second : -1;    } private:    unordered_map<int, unordered_map<int, int>> bids_;    unordered_map<int, set<pair<int, int>>> bidders_;}; 

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 ↗