Algorithm


Problem Name: 855. Exam Room

There is an exam room with n seats in a single row labeled from 0 to n - 1.

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.

Design a class that simulates the mentioned exam room.

Implement the ExamRoom class:

  • ExamRoom(int n) Initializes the object of the exam room with the number of the seats n.
  • int seat() Returns the label of the seat at which the next student will set.
  • 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.

 

Example 1:

Input
["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]
[[10], [], [], [], [], [4], []]
Output
[null, 0, 9, 4, 2, null, 5]

Explanation
ExamRoom examRoom = new ExamRoom(10);
examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0.
examRoom.seat(); // return 9, the student sits at the last seat number 9.
examRoom.seat(); // return 4, the student sits at the last seat number 4.
examRoom.seat(); // return 2, the student sits at the last seat number 2.
examRoom.leave(4);
examRoom.seat(); // return 5, the student sits at the last seat number 5.

 

Constraints:

  • 1 <= n <= 109
  • It is guaranteed that there is a student sitting at seat p.
  • At most 104 calls will be made to seat and leave.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class ExamRoom {
public:
    ExamRoom(int N) {
        this->N = N;
    }
    
    int seat() {
        if (s.size() == 0) {
            s.insert(0);
            return 0;
        } else if (s.size() == 1) {
            int pos = *s.begin()  <  N/2 ? N - 1 : 0;
            s.insert(pos);
            return pos;
        } else {
            int maxDis = -1;
            int res = -1;
            if (!s.count(0)) {
                maxDis = *s.begin() - 1;
                res = 0;
            }
            auto p1 = s.begin();
            auto p2 = p1;
            ++p2;
            while (p2 != s.end()) {
                int pos = (*p2 + *p1)/2;
                int d = min(pos - *p1, *p2 - pos) - 1;
                if (d > maxDis) {
                    maxDis = d;
                    res = (*p1 + *p2)/2;
                }
                ++p1;
                ++p2;
            }
            if (!s.count(N - 1)) {
                auto e = s.end();
                --e;
                int d = N - 1 - *e - 1;
                if (d > maxDis) {
                    res = N - 1;
                }
            }
            s.insert(res);
            return res;
        }
    }
    
    void leave(int p) {
        s.erase(p);
    }
    
private:
    set < int>s;
    int N;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"] [[10], [], [], [], [], [4], []]

Output

x
+
cmd
[null, 0, 9, 4, 2, null, 5]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const ExamRoom = function(n) {
    let a = [];
    return { seat, leave }
    function seat() {
        if (a.length == 0) {
            a.push(0);
            return 0;
        }
        let dis = Math.max(a[0], n - 1 - a[a.length - 1]);
        for (let i = 1; i  <  a.length; i++) dis = Math.max(dis, a[i] - a[i - 1] >> 1);
        if (a[0] == dis) {
            a.unshift(0);
            return 0;
        }
        for (let i = 1; i  <  a.length; i++) {
            if (a[i] - a[i - 1] >> 1 == dis) {
                a.splice(i, 0, a[i] + a[i - 1] >> 1);
                return a[i];
            }
        }
        a.push(n - 1);
        return n - 1;
    }
    function leave(p) {
        for (let i = 0; i  <  a.length; i++) {
            if (a[i] == p) {
                a.splice(i, 1);
                break;
            }
        }
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"] [[10], [], [], [], [], [4], []]

Output

x
+
cmd
[null, 0, 9, 4, 2, null, 5]

#3 Code Example with Python Programming

Code - Python Programming


class ExamRoom:

    def __init__(self, N):
        self.seated, self.n = [], N - 1
        

    def seat(self):
        if not self.seated:
            self.seated += 0,
            return 0
        mx = ind = 0
        for i in range(1, len(self.seated)):
            l, r = self.seated[i - 1], self.seated[i]
            if (r - l) // 2 > mx:
                mx = (r - l) // 2
                ind = l + mx
        if self.seated[-1] != self.n and self.n - self.seated[-1] > mx:
            mx, ind = self.n - self.seated[-1], self.n
        if self.seated[0] >= mx:
            mx, ind = self.seated[0], 0
        self.seated.append(ind)
        self.seated.sort()
        return ind
        
        
    def leave(self, p):
        self.seated.remove(p)
Copy The Code & Try With Live Editor

Input

x
+
cmd
["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"] [[10], [], [], [], [], [4], []]

Output

x
+
cmd
[null, 0, 9, 4, 2, null, 5]
Advertisements

Demonstration


Previous
#854 Leetcode K-Similar Strings Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#856 Leetcode Score of Parentheses Solution in C, C++, Java, JavaScript, Python, C# Leetcode