forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
my-calendar-iii.cpp
52 lines (44 loc) · 1.12 KB
/
my-calendar-iii.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
// Time: O(nlogn) ~ O(n^2)
// Space: O(n)
class MyCalendarThree {
public:
MyCalendarThree() {
events_.emplace(-1, 0);
}
int book(int start, int end) {
auto sit = events_.emplace(start, prev(events_.upper_bound(start))->second).first;
auto eit = events_.emplace(end, prev(events_.upper_bound(end))->second).first;
for (; sit != eit; ++sit) {
count_ = max(count_, ++(sit->second));
}
return count_;
}
private:
map<int, int> events_;
int count_ = 0;
};
// Time: O(n^2)
// Space: O(n)
class MyCalendarThree2 {
public:
MyCalendarThree2() {
}
int book(int start, int end) {
++books_[start];
--books_[end];
int result = 0;
int cnt = 0;
for (const auto &book : books_) {
cnt += book.second;
result = max(result, cnt);
}
return result;
}
private:
map<int, int> books_;
};
/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree obj = new MyCalendarThree();
* int param_1 = obj.book(start,end);
*/