Design Auction System Problem
Description
LeetCode Problem 3815.
You are asked to design an auction system that manages bids from multiple users in real time.
Each bid is associated with a userId, an itemId, and a bidAmount.
Implement the AuctionSystem class:
AuctionSystem(): Initializes theAuctionSystemobject.void addBid(int userId, int itemId, int bidAmount): Adds a new bid foritemIdbyuserIdwithbidAmount. If the sameuserIdalready has a bid onitemId, replace it with the newbidAmount.void updateBid(int userId, int itemId, int newAmount): Updates the existing bid ofuserIdforitemIdtonewAmount. It is guaranteed that this bid exists.void removeBid(int userId, int itemId): Removes the bid ofuserIdforitemId. It is guaranteed that this bid exists.int getHighestBidder(int itemId): Returns theuserIdof the highest bidder foritemId. If multiple users have the same highestbidAmount, return the user with the highestuserId. If no bids exist for the item, return -1.
Example 1:
Input:
["AuctionSystem", "addBid", "addBid", "getHighestBidder", "updateBid", "getHighestBidder", "removeBid", "getHighestBidder", "getHighestBidder"]
[[], [1, 7, 5], [2, 7, 6], [7], [1, 7, 8], [7], [2, 7], [7], [3]]
Output:
[null, null, null, 2, null, 1, null, 1, -1]
Explanation
AuctionSystem auctionSystem = new AuctionSystem(); // Initialize the Auction system
auctionSystem.addBid(1, 7, 5); // User 1 bids 5 on item 7
auctionSystem.addBid(2, 7, 6); // User 2 bids 6 on item 7
auctionSystem.getHighestBidder(7); // return 2 as User 2 has the highest bid
auctionSystem.updateBid(1, 7, 8); // User 1 updates bid to 8 on item 7
auctionSystem.getHighestBidder(7); // return 1 as User 1 now has the highest bid
auctionSystem.removeBid(2, 7); // Remove User 2's bid on item 7
auctionSystem.getHighestBidder(7); // return 1 as User 1 is the current highest bidder
auctionSystem.getHighestBidder(3); // return -1 as no bids exist for item 3
Constraints:
1 <= userId, itemId <= 5 * 10^41 <= bidAmount, newAmount <= 10^9- At most
5 * 10^4total calls toaddBid,updateBid,removeBid, andgetHighestBidder. - The input is generated such that for
updateBidandremoveBid, the bid from the givenuserIdfor the givenitemIdwill be valid.
Solution
Approach: Hash Table + Ordered Set
Keep two structures. users maps each (userId, itemId) pair to that user's current bid, and items[itemId] holds every bid on the item as (bidAmount, userId) pairs in a sorted set. Sorting by amount and then by user id means the largest element is always the highest bidder, with ties going to the larger id.
addBid: if the user already bids on the item, remove the old bid first. Then record the new one in both structures.updateBid: look up the old amount inusers, remove(oldAmount, userId)from the item's set, insert(newAmount, userId)and store the new amount.removeBid: look up the old amount, remove its pair from the set and delete the entry fromusers.getHighestBidder: return the user id of the last element of the item's set, or-1if the set is empty or missing.
The Python, JavaScript and Go tabs have no built-in sorted set, so they use a max-heap per item instead. Replaced and removed bids stay in the heap as stale entries, and getHighestBidder pops the top while it no longer matches the user's current bid.
Complexity: each operation takes O(log m) time, where m is the number of bids on the item. Space is O(n) for n bids in total.
import heapq
class AuctionSystem:
def __init__(self):
self.bids = {} # (userId, itemId) -> current bid amount
self.heaps = {} # itemId -> max-heap of (-amount, -userId), may hold stale entries
def addBid(self, userId: int, itemId: int, bidAmount: int) -> None:
self.bids[(userId, itemId)] = bidAmount
heapq.heappush(self.heaps.setdefault(itemId, []), (-bidAmount, -userId))
def updateBid(self, userId: int, itemId: int, newAmount: int) -> None:
self.addBid(userId, itemId, newAmount)
def removeBid(self, userId: int, itemId: int) -> None:
del self.bids[(userId, itemId)]
def getHighestBidder(self, itemId: int) -> int:
h = self.heaps.get(itemId)
# drop entries whose bid was replaced or removed
while h and self.bids.get((-h[0][1], itemId)) != -h[0][0]:
heapq.heappop(h)
return -h[0][1] if h else -1
# Your AuctionSystem object will be instantiated and called as such:
# obj = AuctionSystem()
# obj.addBid(userId,itemId,bidAmount)
# obj.updateBid(userId,itemId,newAmount)
# obj.removeBid(userId,itemId)
# param_4 = obj.getHighestBidder(itemId)