-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0732.cpp
More file actions
41 lines (32 loc) · 901 Bytes
/
Copy path0732.cpp
File metadata and controls
41 lines (32 loc) · 901 Bytes
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
// Map solution
class MyCalendarThree {
map<int, int> mp;
public:
int book(int startTime, int endTime) {
mp[startTime]++;
mp[endTime]--;
int res = 0, acc = 0;
for (const auto [_, add] : mp) {
res = max(res, acc += add);
}
return res;
}
};
// Vector solution
class MyCalendarThree {
using type_t = pair<int, int>;
vector<type_t> vec;
public:
int book(int startTime, int endTime) {
const type_t start = {startTime, 1};
const type_t end = {endTime, -1};
// trick to insert into a sorted vector
vec.insert(upper_bound(vec.begin(), vec.end(), start), start);
vec.insert(upper_bound(vec.begin(), vec.end(), end), end);
int res = 0, acc = 0;
for (const auto [_, add] : vec) {
res = max(res, acc += add);
}
return res;
}
};