Problem solution · Python

Design Auction System

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

  • 131 lines of Python from the credited upstream file design-auction-system.py.
  • The implementation visibly relies on sequence storage, hash lookup, 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 Auction System · PythonPython
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) import collectionsimport heapq  # hash table, heapclass AuctionSystem(object):     def __init__(self):        self.__bids = collections.defaultdict(lambda: collections.defaultdict(int))        self.__bidders = collections.defaultdict(list)             def addBid(self, userId, itemId, bidAmount):        """        :type userId: int        :type itemId: int        :type bidAmount: int        :rtype: None        """        self.__bids[itemId][userId] = bidAmount        heapq.heappush(self.__bidders[itemId], (-bidAmount, -userId))             def updateBid(self, userId, itemId, newAmount):        """        :type userId: int        :type itemId: int        :type newAmount: int        :rtype: None        """        self.addBid(userId, itemId, newAmount)      def removeBid(self, userId, itemId):        """        :type userId: int        :type itemId: int        :rtype: None        """        del self.__bids[itemId][userId]        if not self.__bids[itemId]:            del self.__bids[itemId]      def getHighestBidder(self, itemId):        """        :type itemId: int        :rtype: int        """        if itemId not in self.__bidders:            return -1        while self.__bidders[itemId]:            p, u = self.__bidders[itemId][0]            p, u = -p, -u            if self.__bids[itemId][u] == p:                return u            heapq.heappop(self.__bidders[itemId])        del self.__bidders[itemId]        return -1        # Time:  ctor:             O(1)#        addBid:           O(logn)#        updateBid:        O(logn)#        removeBid:        O(logn)#        getHighestBidder: O(1)# Space: O(n)import collectionsfrom sortedcontainers import SortedList  # hash table, sorted listclass AuctionSystem2(object):     def __init__(self):        self.__bids = collections.defaultdict(lambda: collections.defaultdict(int))        self.__bidders = collections.defaultdict(SortedList)             def addBid(self, userId, itemId, bidAmount):        """        :type userId: int        :type itemId: int        :type bidAmount: int        :rtype: None        """        if userId in self.__bids[itemId]:            self.__bidders[itemId].remove((self.__bids[itemId][userId], userId))        self.__bids[itemId][userId] = bidAmount        self.__bidders[itemId].add((bidAmount, userId))             def updateBid(self, userId, itemId, newAmount):        """        :type userId: int        :type itemId: int        :type newAmount: int        :rtype: None        """        self.addBid(userId, itemId, newAmount)      def removeBid(self, userId, itemId):        """        :type userId: int        :type itemId: int        :rtype: None        """        self.__bidders[itemId].remove((self.__bids[itemId][userId], userId))        if not self.__bidders[itemId]:            del self.__bidders[itemId]        del self.__bids[itemId][userId]        if not self.__bids[itemId]:            del self.__bids[itemId]      def getHighestBidder(self, itemId):        """        :type itemId: int        :rtype: int        """        return self.__bidders[itemId][-1][1] if itemId in self.__bidders else -1         

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 ↗