3815. Design Auction System Medium
1/**
2 * [3815] Design Auction System
3 *
4 * You are asked to design an auction system that manages bids from multiple users in real time.
5 * Each bid is associated with a userId, an itemId, and a bidAmount.
6 * Implement the AuctionSystem class:
7 *
8 * AuctionSystem(): Initializes the AuctionSystem object.
9 * 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.
10 * 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.
11 * void removeBid(int userId, int itemId): Removes the bid of userId for itemId. It is guaranteed that this bid exists.
12 * 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.
13 *
14 *
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input:<br />
18 * <span class="example-io">["AuctionSystem", "addBid", "addBid", "getHighestBidder", "updateBid", "getHighestBidder", "removeBid", "getHighestBidder", "getHighestBidder"]<br />
19 * [[], [1, 7, 5], [2, 7, 6], [7], [1, 7, 8], [7], [2, 7], [7], [3]]</span>
20 * Output:<br />
21 * <span class="example-io">[null, null, null, 2, null, 1, null, 1, -1] </span>
22 * Explanation
23 * AuctionSystem auctionSystem = new AuctionSystem(); // Initialize the Auction system<br />
24 * auctionSystem.addBid(1, 7, 5); // User 1 bids 5 on item 7<br />
25 * auctionSystem.addBid(2, 7, 6); // User 2 bids 6 on item 7<br />
26 * auctionSystem.getHighestBidder(7); // return 2 as User 2 has the highest bid<br />
27 * auctionSystem.updateBid(1, 7, 8); // User 1 updates bid to 8 on item 7<br />
28 * auctionSystem.getHighestBidder(7); // return 1 as User 1 now has the highest bid<br />
29 * auctionSystem.removeBid(2, 7); // Remove User 2's bid on item 7<br />
30 * auctionSystem.getHighestBidder(7); // return 1 as User 1 is the current highest bidder<br />
31 * auctionSystem.getHighestBidder(3); // return -1 as no bids exist for item 3</div>
32 *
33 * Constraints:
34 *
35 * 1 <= userId, itemId <= 5 * 10^4
36 * 1 <= bidAmount, newAmount <= 10^9
37 * At most 5 * 10^4 total calls to addBid, updateBid, removeBid, and getHighestBidder.
38 * The input is generated such that for updateBid and removeBid, the bid from the given userId for the given itemId will be valid.
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/design-auction-system/
44// discuss: https://leetcode.com/problems/design-auction-system/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48struct AuctionSystem {
49 false
50 }
51
52
53/**
54 * `&self` means the method takes an immutable reference.
55 * If you need a mutable reference, change it to `&mut self` instead.
56 */
57impl AuctionSystem {
58
59 fn new() -> Self {
60
61 }
62
63 fn add_bid(&self, user_id: i32, item_id: i32, bid_amount: i32) {
64
65 }
66
67 fn update_bid(&self, user_id: i32, item_id: i32, new_amount: i32) {
68
69 }
70
71 fn remove_bid(&self, user_id: i32, item_id: i32) {
72
73 }
74
75 fn get_highest_bidder(&self, item_id: i32) -> i32 {
76
77 }
78}
79
80/**
81 * Your AuctionSystem object will be instantiated and called as such:
82 * let obj = AuctionSystem::new();
83 * obj.add_bid(userId, itemId, bidAmount);
84 * obj.update_bid(userId, itemId, newAmount);
85 * obj.remove_bid(userId, itemId);
86 * let ret_4: i32 = obj.get_highest_bidder(itemId);
87 */
88
89// submission codes end
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn test_3815() {
97 }
98}
99Back
© 2026 bowen.ge All Rights Reserved.