-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path855. Exam Room.cpp
62 lines (58 loc) · 1.4 KB
/
855. Exam Room.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
};
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom obj = new ExamRoom(N);
* int param_1 = obj.seat();
* obj.leave(p);
*/