|
| 1 | +/** |
| 2 | + * 855. Exam Room |
| 3 | + * https://leetcode.com/problems/exam-room/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * There is an exam room with n seats in a single row labeled from 0 to n - 1. |
| 7 | + * |
| 8 | + * When a student enters the room, they must sit in the seat that maximizes the distance to the |
| 9 | + * closest person. If there are multiple such seats, they sit in the seat with the lowest number. |
| 10 | + * If no one is in the room, then the student sits at seat number 0. |
| 11 | + * |
| 12 | + * Design a class that simulates the mentioned exam room. |
| 13 | + * |
| 14 | + * Implement the ExamRoom class: |
| 15 | + * - ExamRoom(int n) Initializes the object of the exam room with the number of the seats n. |
| 16 | + * - int seat() Returns the label of the seat at which the next student will set. |
| 17 | + * - void leave(int p) Indicates that the student sitting at seat p will leave the room. It is |
| 18 | + * guaranteed that there will be a student sitting at seat p. |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {number} n - Number of seats in the exam room |
| 23 | + */ |
| 24 | +var ExamRoom = function(n) { |
| 25 | + this.n = n; |
| 26 | + this.seats = []; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * @return {number} |
| 31 | + */ |
| 32 | +ExamRoom.prototype.seat = function() { |
| 33 | + if (this.seats.length === 0) { |
| 34 | + this.seats.push(0); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + |
| 38 | + let maxDistance = this.seats[0]; |
| 39 | + let chosenSeat = 0; |
| 40 | + |
| 41 | + for (let i = 1; i < this.seats.length; i++) { |
| 42 | + const distance = Math.floor((this.seats[i] - this.seats[i-1]) / 2); |
| 43 | + if (distance > maxDistance) { |
| 44 | + maxDistance = distance; |
| 45 | + chosenSeat = Math.floor((this.seats[i] + this.seats[i-1]) / 2); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const endDistance = this.n - 1 - this.seats[this.seats.length - 1]; |
| 50 | + if (endDistance > maxDistance) { |
| 51 | + chosenSeat = this.n - 1; |
| 52 | + maxDistance = endDistance; |
| 53 | + } |
| 54 | + |
| 55 | + const insertPosition = this.findInsertPosition(chosenSeat); |
| 56 | + this.seats.splice(insertPosition, 0, chosenSeat); |
| 57 | + |
| 58 | + return chosenSeat; |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * @param {number} p |
| 63 | + * @return {void} |
| 64 | + */ |
| 65 | +ExamRoom.prototype.leave = function(p) { |
| 66 | + const index = this.seats.indexOf(p); |
| 67 | + this.seats.splice(index, 1); |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * @param {number} seat |
| 72 | + * @return {number} |
| 73 | + */ |
| 74 | +ExamRoom.prototype.findInsertPosition = function(seat) { |
| 75 | + let left = 0; |
| 76 | + let right = this.seats.length - 1; |
| 77 | + |
| 78 | + while (left <= right) { |
| 79 | + const mid = Math.floor((left + right) / 2); |
| 80 | + if (this.seats[mid] < seat) { |
| 81 | + left = mid + 1; |
| 82 | + } else { |
| 83 | + right = mid - 1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return left; |
| 88 | +}; |
0 commit comments