|
| 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 | + */ |
| 38 | +pub 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 | + |
| 45 | +use std::collections::HashSet; |
| 46 | + |
| 47 | +struct ExamRoom { |
| 48 | + seats: HashSet<i32>, |
| 49 | + capacity: i32, |
| 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 | + */ |
| 57 | +impl ExamRoom { |
| 58 | + |
| 59 | + fn new(n: i32) -> Self { |
| 60 | + ExamRoom { |
| 61 | + seats : HashSet::new(), |
| 62 | + capacity: n, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fn seat(&mut self) -> i32 { |
| 67 | + if self.seats.len() == 0 { |
| 68 | + self.seats.insert(0); |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + let (mut left, mut right) = (0, 0); |
| 73 | + let mut max_distance = 0; |
| 74 | + let mut pre = 0; |
| 75 | + for (i, item) in self.seats.iter().enumerate() { |
| 76 | + println!("i is {}, item is {}", i, *item); |
| 77 | + if pre == 0 { |
| 78 | + if *item - pre > max_distance { |
| 79 | + left = pre; |
| 80 | + right = *item; |
| 81 | + max_distance = right - left; |
| 82 | + } |
| 83 | + } else { |
| 84 | + if (*item - pre) / 2 > max_distance { |
| 85 | + left = pre; |
| 86 | + right = *item; |
| 87 | + max_distance = (right - left) / 2; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pre = *item; |
| 92 | + } |
| 93 | + |
| 94 | + if self.capacity - 1 - pre > max_distance { |
| 95 | + left = pre; |
| 96 | + right = self.capacity - 1; |
| 97 | + max_distance = right - left; |
| 98 | + } |
| 99 | + println!("left is {}, right is {}", left, right); |
| 100 | + |
| 101 | + if left == 0 { |
| 102 | + self.seats.insert(left); |
| 103 | + return left; |
| 104 | + } else if right == self.capacity - 1 { |
| 105 | + self.seats.insert(right); |
| 106 | + return right; |
| 107 | + } else { |
| 108 | + let res = left + (right - left) / 2; |
| 109 | + self.seats.insert(res); |
| 110 | + return res; |
| 111 | + } |
| 112 | + |
| 113 | + 0 |
| 114 | + } |
| 115 | + |
| 116 | + fn leave(&mut self, p: i32) { |
| 117 | + self.seats.remove(&p); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Your ExamRoom object will be instantiated and called as such: |
| 123 | + * let obj = ExamRoom::new(n); |
| 124 | + * let ret_1: i32 = obj.seat(); |
| 125 | + * obj.leave(p); |
| 126 | + */ |
| 127 | + |
| 128 | +// submission codes end |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +mod tests { |
| 132 | + use super::*; |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn test_855() { |
| 136 | + let mut obj = ExamRoom::new(10); |
| 137 | + let ret_1: i32 = obj.seat(); |
| 138 | + let ret_2: i32 = obj.seat(); |
| 139 | + let ret_3: i32 = obj.seat(); |
| 140 | + let ret_4: i32 = obj.seat(); |
| 141 | + obj.leave(4); |
| 142 | + let ret_5 = obj.seat(); |
| 143 | + println!("{} {} {} {} {}", ret_1, ret_2, ret_3, ret_4, ret_5); |
| 144 | + } |
| 145 | +} |
0 commit comments