3815. Design Auction System
Description
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 systemauctionSystem.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 * 1041 <= bidAmount, newAmount <= 109- At most
5 * 104total calls toaddBid,updateBid,removeBid, andgetHighestBidder. - The input is generated such that for
updateBidandremoveBid, the bid from the givenuserIdfor the givenitemIdwill be valid.
Solutions
Solution 1: Hash Table + Ordered Set
We define two hash tables. items is used to store all bid information for each item, where items[itemId] stores an ordered set. Each element in the set is a tuple (bidAmount, userId), representing a user's bid amount for that item. Since we need to quickly retrieve the user with the highest bid, this ordered set needs to be sorted by bid amount in ascending order. If bid amounts are identical, they are sorted by user ID in ascending order. The other hash table users is used to store the bid information of each user for each item, where users[userId][itemId] stores the user's bid amount for that item.
For the addBid(userId, itemId, bidAmount) operation, we first check if the user has already placed a bid on the item. If they have, we call the removeBid(userId, itemId) method to remove the original bid; then we add the new bid information to users and items.
For the updateBid(userId, itemId, newAmount) operation, we first retrieve the user's original bid amount for the item from users, then remove the corresponding tuple (oldAmount, userId) from items, add the new bid information to items, and update the bid amount in users.
For the removeBid(userId, itemId) operation, we first retrieve the user's original bid amount for the item from users, then remove the corresponding tuple (oldAmount, userId) from items, and finally delete the user's bid information for that item from users.
For the getHighestBidder(itemId) operation, we first check if items[itemId] is empty. If it is, we return -1; otherwise, we return the user ID of the last element in the ordered set, which corresponds to the highest bidder.
In terms of time complexity, each operation takes \(O(\log m)\) time, where \(m\) is the number of bids for the current item. The space complexity is \(O(n)\), where \(n\) is the total number of bids.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
1 | |