855. Exam Room Medium

@problem@discussion
#Design#Ordered Set



1/**
2 * [855] Exam Room
3 *
4 * There is an exam room with n seats in a single row labeled from 0 to n - 1.
5 * When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits at seat number 0.
6 * Design a class that simulates the mentioned exam room.
7 * Implement the ExamRoom class:
8 * 
9 * 	ExamRoom(int n) Initializes the object of the exam room with the number of the seats n.
10 * 	int seat() Returns the label of the seat at which the next student will set.
11 * 	void leave(int p) Indicates that the student sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at seat p.
12 * 
13 *  
14 * Example 1:
15 * 
16 * Input
17 * ["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]
18 * [[10], [], [], [], [], [4], []]
19 * Output
20 * [null, 0, 9, 4, 2, null, 5]
21 * Explanation
22 * ExamRoom examRoom = new ExamRoom(10);
23 * examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0.
24 * examRoom.seat(); // return 9, the student sits at the last seat number 9.
25 * examRoom.seat(); // return 4, the student sits at the last seat number 4.
26 * examRoom.seat(); // return 2, the student sits at the last seat number 2.
27 * examRoom.leave(4);
28 * examRoom.seat(); // return 5, the student sits at the last seat number 5.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= n <= 10^9
34 * 	It is guaranteed that there is a student sitting at seat p.
35 * 	At most 10^4 calls will be made to seat and leave.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/exam-room/
41// discuss: https://leetcode.com/problems/exam-room/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45struct ExamRoom {
46        vec![]
47    }
48
49
50/** 
51 * `&self` means the method takes an immutable reference.
52 * If you need a mutable reference, change it to `&mut self` instead.
53 */
54impl ExamRoom {
55
56    fn new(n: i32) -> Self {
57        
58    }
59    
60    fn seat(&self) -> i32 {
61        
62    }
63    
64    fn leave(&self, p: i32) {
65        
66    }
67}
68
69/**
70 * Your ExamRoom object will be instantiated and called as such:
71 * let obj = ExamRoom::new(n);
72 * let ret_1: i32 = obj.seat();
73 * obj.leave(p);
74 */
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_855() {
84    }
85}
86


Back
© 2025 bowen.ge All Rights Reserved.