-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
63 lines (55 loc) · 1.45 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <climits>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/**
* Unlike CalendarOne, in CalendarTwo, we can add an event if it does not cause
* a triple booking, i.e. when three events have some non-empty intersection.
*/
constexpr int kMaximumOverlap = 2;
class MyCalendarTwo {
private:
// Map of {time: count}
std::map<int, int> bookings;
public:
MyCalendarTwo() {}
bool book(int start, int end) {
// Line Sweep. Find the prefix sum in between [start, end).
// The prefix sum will indicate how many previous intervals started in
// the interval [start, end).
++bookings[start];
--bookings[end];
int prefix{0};
// Seemed a little counter-intuitive at first, where we iterate from the
// start. But the prefixSum is incremented by start and decremented by the
// end.
for (const auto& [time, count] : bookings) {
prefix += count;
if (prefix > kMaximumOverlap) {
--bookings[start];
++bookings[end];
return false;
}
if (time >= end) {
break;
}
}
return true;
}
};
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo* obj = new MyCalendarTwo();
* bool param_1 = obj->book(start,end);
*/