Exam Room

medium design sorted-set ordered-set

Problem

Seats are 0..n-1. seat() returns the seat that maximizes distance to the nearest occupied seat (preferring lower indices on ties). leave(p) frees seat p.

Inputn = 10; calls: seat(), seat(), seat(), seat(), leave(4), seat()
Output[0, 9, 4, 2, 5]
Each seat picks the largest gap; ties broken by smaller index.

from sortedcontainers import SortedList

class ExamRoom:
    def __init__(self, n):
        self.n = n
        self.seats = SortedList()
    def seat(self):
        if not self.seats:
            self.seats.add(0); return 0
        best, pos = self.seats[0], 0
        for i in range(1, len(self.seats)):
            d = (self.seats[i] - self.seats[i - 1]) // 2
            if d > best: best, pos = d, self.seats[i - 1] + d
        if self.n - 1 - self.seats[-1] > best:
            pos = self.n - 1
        self.seats.add(pos); return pos
    def leave(self, p):
        self.seats.remove(p)
class ExamRoom {
  constructor(n) { this.n = n; this.seats = []; }
  seat() {
    if (this.seats.length === 0) { this.seats.push(0); return 0; }
    let best = this.seats[0], pos = 0;
    for (let i = 1; i < this.seats.length; i++) {
      const d = (this.seats[i] - this.seats[i - 1]) >> 1;
      if (d > best) { best = d; pos = this.seats[i - 1] + d; }
    }
    if (this.n - 1 - this.seats[this.seats.length - 1] > best) pos = this.n - 1;
    const idx = this.seats.findIndex(s => s > pos);
    if (idx === -1) this.seats.push(pos); else this.seats.splice(idx, 0, pos);
    return pos;
  }
  leave(p) { this.seats.splice(this.seats.indexOf(p), 1); }
}
import java.util.*;
class ExamRoom {
  int n; TreeSet<Integer> seats = new TreeSet<>();
  public ExamRoom(int n) { this.n = n; }
  public int seat() {
    if (seats.isEmpty()) { seats.add(0); return 0; }
    int best = seats.first(), pos = 0, prev = -1;
    for (int s : seats) {
      if (prev >= 0) {
        int d = (s - prev) / 2;
        if (d > best) { best = d; pos = prev + d; }
      }
      prev = s;
    }
    if (n - 1 - seats.last() > best) pos = n - 1;
    seats.add(pos);
    return pos;
  }
  public void leave(int p) { seats.remove(p); }
}
#include <bits/stdc++.h>
using namespace std;
class ExamRoom {
public:
    int n;
    set<int> seats;
    ExamRoom(int n_) : n(n_) {}
    int seat() {
        if (seats.empty()) { seats.insert(0); return 0; }
        int best = *seats.begin(), pos = 0, prev = -1;
        for (int s : seats) {
            if (prev >= 0) {
                int d = (s - prev) / 2;
                if (d > best) { best = d; pos = prev + d; }
            }
            prev = s;
        }
        if (n - 1 - *seats.rbegin() > best) pos = n - 1;
        seats.insert(pos);
        return pos;
    }
    void leave(int p) { seats.erase(p); }
};
Time: O(P) per seat() Space: O(P)