class ExamRoom {
public:
ExamRoom(int n) {
}
int seat() {
}
void leave(int p) {
}
};
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom* obj = new ExamRoom(n);
* int param_1 = obj->seat();
* obj->leave(p);
*/
class ExamRoom {
int N;
TreeSet<Integer> students;
public ExamRoom(int N) {
this.N = N;
students = new TreeSet();
}
public int seat() {
//Let's determine student, the position of the next
//student to sit down.
int student = 0;
if (students.size() > 0) {
//Tenatively, dist is the distance to the closest student,
//which is achieved by sitting in the position 'student'.
//We start by considering the left-most seat.
int dist = students.first();
Integer prev = null;
for (Integer s: students) {
if (prev != null) {
//For each pair of adjacent students in positions (prev, s),
//d is the distance to the closest student;
//achieved at position prev + d.
int d = (s - prev) / 2;
if (d > dist) {
dist = d;
student = prev + d;
}
}
prev = s;
}
//Considering the right-most seat.
if (N - 1 - students.last() > dist)
student = N - 1;
}
//Add the student to our sorted TreeSet of positions.
students.add(student);
return student;
}
public void leave(int p) {
students.remove(p);
}
}
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom obj = new ExamRoom(n);
* int param_1 = obj.seat();
* obj.leave(p);
*/
class ExamRoom(object):
def __init__(self, N):
self.N = N
self.students = []
def seat(self):
# Let's determine student, the position of the next
# student to sit down.
if not self.students:
student = 0
else:
# Tenatively, dist is the distance to the closest student,
# which is achieved by sitting in the position 'student'.
# We start by considering the left-most seat.
dist, student = self.students[0], 0
for i, s in enumerate(self.students):
if i:
prev = self.students[i-1]
# For each pair of adjacent students in positions (prev, s),
# d is the distance to the closest student;
# achieved at position prev + d.
d = (s - prev) // 2
if d > dist:
dist, student = d, prev + d
# Considering the right-most seat.
d = self.N - 1 - self.students[-1]
if d > dist:
student = self.N - 1
# Add the student to our sorted list of positions.
bisect.insort(self.students, student)
return student
def leave(self, p):
self.students.remove(p)
# Your ExamRoom object will be instantiated and called as such:
# obj = ExamRoom(n)
# param_1 = obj.seat()
# obj.leave(p)