- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- 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.
Use this to learn the idea, then write your own version.
1234567 8import collections9import heapq10 11 1213class AuctionSystem(object):14 15 def __init__(self):16 self.__bids = collections.defaultdict(lambda: collections.defaultdict(int))17 self.__bidders = collections.defaultdict(list)18 19 20 def addBid(self, userId, itemId, bidAmount):21 """22 :type userId: int23 :type itemId: int24 :type bidAmount: int25 :rtype: None26 """27 self.__bids[itemId][userId] = bidAmount28 heapq.heappush(self.__bidders[itemId], (-bidAmount, -userId))29 30 31 def updateBid(self, userId, itemId, newAmount):32 """33 :type userId: int34 :type itemId: int35 :type newAmount: int36 :rtype: None37 """38 self.addBid(userId, itemId, newAmount)39 40 41 def removeBid(self, userId, itemId):42 """43 :type userId: int44 :type itemId: int45 :rtype: None46 """47 del self.__bids[itemId][userId]48 if not self.__bids[itemId]:49 del self.__bids[itemId]50 51 52 def getHighestBidder(self, itemId):53 """54 :type itemId: int55 :rtype: int56 """57 if itemId not in self.__bidders:58 return -159 while self.__bidders[itemId]:60 p, u = self.__bidders[itemId][0]61 p, u = -p, -u62 if self.__bids[itemId][u] == p:63 return u64 heapq.heappop(self.__bidders[itemId])65 del self.__bidders[itemId]66 return -1 67 68 69707172737475import collections76from sortedcontainers import SortedList77 78 7980class AuctionSystem2(object):81 82 def __init__(self):83 self.__bids = collections.defaultdict(lambda: collections.defaultdict(int))84 self.__bidders = collections.defaultdict(SortedList)85 86 87 def addBid(self, userId, itemId, bidAmount):88 """89 :type userId: int90 :type itemId: int91 :type bidAmount: int92 :rtype: None93 """94 if userId in self.__bids[itemId]:95 self.__bidders[itemId].remove((self.__bids[itemId][userId], userId))96 self.__bids[itemId][userId] = bidAmount97 self.__bidders[itemId].add((bidAmount, userId))98 99 100 def updateBid(self, userId, itemId, newAmount):101 """102 :type userId: int103 :type itemId: int104 :type newAmount: int105 :rtype: None106 """107 self.addBid(userId, itemId, newAmount)108 109 110 def removeBid(self, userId, itemId):111 """112 :type userId: int113 :type itemId: int114 :rtype: None115 """116 self.__bidders[itemId].remove((self.__bids[itemId][userId], userId))117 if not self.__bidders[itemId]:118 del self.__bidders[itemId]119 del self.__bids[itemId][userId]120 if not self.__bids[itemId]:121 del self.__bids[itemId]122 123 124 def getHighestBidder(self, itemId):125 """126 :type itemId: int127 :rtype: int128 """129 return self.__bidders[itemId][-1][1] if itemId in self.__bidders else -1130 131