-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-calendar-ii.cpp
57 lines (43 loc) · 1.27 KB
/
my-calendar-ii.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
class MyCalendarTwo {
public:
// https://www.cnblogs.com/grandyang/p/7968035.html
MyCalendarTwo() {
}
bool book(int start, int end) {
m[start]++;
m[end]--;
int cnt=0;
for(const auto &n:m){
cnt+=n.second;
if(cnt>=3){
m[start]--;
m[end]++;
return false;
}
}
return true;
}
map<int,int> m;
pair<int,int> cal_overlap(const pair<int,int> &a, const pair<int,int> &b){
return {max(a.first,b.first),min(a.second,b.second)};
}
bool book1(int start, int end) {
pair<int,int> tmp={start,end};
for(const auto v2:vec2){
auto t=cal_overlap(tmp,v2);
if(t.first<t.second) return false;
}
for(const auto v1:vec1){
auto t=cal_overlap({start,end},v1);
if(t.first<t.second) vec2.push_back(t); // ??????????if
}
vec1.push_back(tmp);
return true;
}
vector<pair<int,int>> vec1,vec2;
};
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo* obj = new MyCalendarTwo();
* bool param_1 = obj->book(start,end);
*/