1845. Seat Reservation Manager Medium

@problem@discussion
#Design#Heap (Priority Queue)



1/**
2 * [1845] Seat Reservation Manager
3 *
4 * Design a system that manages the reservation state of n seats that are numbered from 1 to n.
5 * Implement the SeatManager class:
6 * 
7 * 	SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially available.
8 * 	int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
9 * 	void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
10 * 
11 *  
12 * Example 1:
13 * 
14 * Input
15 * ["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
16 * [[5], [], [], [2], [], [], [], [], [5]]
17 * Output
18 * [null, 1, 2, null, 2, 3, 4, 5, null]
19 * Explanation
20 * SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
21 * seatManager.reserve();    // All seats are available, so return the lowest numbered seat, which is 1.
22 * seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
23 * seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
24 * seatManager.reserve();    // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
25 * seatManager.reserve();    // The available seats are [3,4,5], so return the lowest of them, which is 3.
26 * seatManager.reserve();    // The available seats are [4,5], so return the lowest of them, which is 4.
27 * seatManager.reserve();    // The only available seat is seat 5, so return 5.
28 * seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= n <= 10^5
34 * 	1 <= seatNumber <= n
35 * 	For each call to reserve, it is guaranteed that there will be at least one unreserved seat.
36 * 	For each call to unreserve, it is guaranteed that seatNumber will be reserved.
37 * 	At most 10^5 calls in total will be made to reserve and unreserve.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/seat-reservation-manager/
43// discuss: https://leetcode.com/problems/seat-reservation-manager/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47struct SeatManager {
48        false
49    }
50
51
52/** 
53 * `&self` means the method takes an immutable reference.
54 * If you need a mutable reference, change it to `&mut self` instead.
55 */
56impl SeatManager {
57
58    fn new(n: i32) -> Self {
59        
60    }
61    
62    fn reserve(&self) -> i32 {
63        
64    }
65    
66    fn unreserve(&self, seat_number: i32) {
67        
68    }
69}
70
71/**
72 * Your SeatManager object will be instantiated and called as such:
73 * let obj = SeatManager::new(n);
74 * let ret_1: i32 = obj.reserve();
75 * obj.unreserve(seatNumber);
76 */
77
78// submission codes end
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_1845() {
86    }
87}
88


Back
© 2025 bowen.ge All Rights Reserved.