-
Notifications
You must be signed in to change notification settings - Fork 3
/
855.cpp
46 lines (43 loc) · 1.09 KB
/
855.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
class ExamRoom {
public:
set<int> st;
int n;
ExamRoom(int n) {
this->n = n;
}
int seat() {
int current = 0;
int maxInterval = 0;
int idx = 0;
for (auto& position : st) {
if (current == 0) {
if (position - current > maxInterval) {
maxInterval = position - current;
idx = 0;
}
}
else {
if ((position - current + 1) / 2 > maxInterval) {
maxInterval = (position - current + 1) / 2;
idx = current + maxInterval - 1;
}
}
current = position + 1;
}
if (current > 0 && n - current > maxInterval) {
maxInterval = n - current;
idx = n - 1;
}
st.insert(idx);
return idx;
}
void leave(int p) {
st.erase(p);
}
};
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom* obj = new ExamRoom(n);
* int param_1 = obj->seat();
* obj->leave(p);
*/