[LeetCode] Design Auction System

3815. Design Auction System

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 the AuctionSystem object.
  • void addBid(int userId, int itemId, int bidAmount): Adds a new bid for itemId by userId with bidAmount. If the same userId already has a bid on itemId, replace it with the new bidAmount.
  • void updateBid(int userId, int itemId, int newAmount): Updates the existing bid of userId for itemId to newAmount. It is guaranteed that this bid exists.
  • void removeBid(int userId, int itemId): Removes the bid of userId for itemId. It is guaranteed that this bid exists.
  • int getHighestBidder(int itemId): Returns the userId of the highest bidder for itemId. If multiple users have the same highest bidAmount, return the user with the highest userId. If no bids exist for the item, return -1.
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

class AuctionSystem {
unordered_map<int, unordered_map<int, int>> bids;
unordered_map<int, multiset<pair<int,int>>> ords;
bool has(int userId, int itemId) {
if(!bids.count(userId)) return false;
return bids[userId].count(itemId);
}
void del(int userId, int itemId) {
ords[itemId].erase({bids[userId][itemId], userId});
if(ords[itemId].size() == 0) ords.erase(itemId);
bids[userId].erase(itemId);
if(bids[userId].size() == 0) bids.erase(userId);
}
void add(int userId, int itemId, int amount) {
bids[userId][itemId] = amount;
ords[itemId].insert({amount, userId});
}
public:
AuctionSystem() {}

void addBid(int userId, int itemId, int bidAmount) {
if(has(userId,itemId)) {
del(userId,itemId);
}
add(userId,itemId,bidAmount);
}

void updateBid(int userId, int itemId, int newAmount) {
if(!has(userId,itemId)) return;
del(userId,itemId);
add(userId,itemId,newAmount);
}

void removeBid(int userId, int itemId) {
del(userId,itemId);
}

int getHighestBidder(int itemId) {
if(!ords.count(itemId)) return -1;
return prev(end(ords[itemId]))->second;
}
};

/**
* Your AuctionSystem object will be instantiated and called as such:
* AuctionSystem* obj = new AuctionSystem();
* obj->addBid(userId,itemId,bidAmount);
* obj->updateBid(userId,itemId,newAmount);
* obj->removeBid(userId,itemId);
* int param_4 = obj->getHighestBidder(itemId);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/design-auction-system/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.