-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamRoom.cpp
More file actions
49 lines (43 loc) · 1.04 KB
/
examRoom.cpp
File metadata and controls
49 lines (43 loc) · 1.04 KB
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
// Source: https://leetcode.com/problems/exam-room/
// Author: Miao Zhang
// Date: 2021-03-18
class ExamRoom {
public:
ExamRoom(int N) : N_(N) {
}
int seat() {
int res;
if (s_.empty()) res = 0;
else {
int dist = *s_.begin();
res = 0;
auto left = s_.begin();
auto right = left;
while (left != s_.end()) {
++right;
int l = *left;
int r = right != s_.end() ? *right : (2 * (N_ - 1) - *left);
int d = (r - l) / 2;
if (d > dist) {
dist = d;
res = l + d;
}
left = right;
}
}
s_.insert(res);
return res;
}
void leave(int p) {
s_.erase(p);
}
private:
const int N_;
set<int> s_;
};
/**
* Your ExamRoom object will be instantiated and called as such:
* ExamRoom* obj = new ExamRoom(N);
* int param_1 = obj->seat();
* obj->leave(p);
*/