forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
56 lines (41 loc) · 1.06 KB
/
main2.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
/// Source : https://leetcode.com/problems/my-calendar-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-27
#include <iostream>
#include <vector>
#include <cassert>
#include <map>
using namespace std;
/// Boundary Count
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class MyCalendarThree {
private:
map<int, int> boundary;
public:
MyCalendarThree() {
boundary.clear();
}
int book(int start, int end) {
boundary[start] += 1;
boundary[end] -= 1;
int res = 0;
int count = 0;
for(const pair<int, int>& p: boundary){
count += p.second;
res = max(res, count);
}
return res;
}
};
int main() {
MyCalendarThree calendar;
cout << calendar.book(10, 20) << endl; // 1
cout << calendar.book(50, 60) << endl; // 1
cout << calendar.book(10, 40) << endl; // 2
cout << calendar.book(5, 15) << endl; // 3
cout << calendar.book(5, 10) << endl; // 3
cout << calendar.book(25, 55) << endl; // 3
cout << endl;
return 0;
}