generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
38 lines (31 loc) · 855 Bytes
/
solution.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
#include <algorithm>
#include <vector>
class MyCalendar {
private:
struct Schedule {
int start;
int end;
};
std::vector<Schedule> schedules;
public:
MyCalendar() : schedules{} {}
bool book(int start, int end) {
const auto it = std::lower_bound(
schedules.begin(), schedules.end(), end,
[](const Schedule& schedule, int val) {
return schedule.end < val;
});
if (it == schedules.end()) {
if (!schedules.empty() && schedules.back().end > start) return false;
schedules.push_back({.start = start, .end = end});
return true;
}
if (it->start < end) return false;
if (it != schedules.begin()) {
const auto prev = std::prev(it);
if (prev->end > start) return false;
}
schedules.insert(it, {.start = start, .end = end});
return true;
}
};