leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0732.cpp (901B)


0 // Map solution
1 class MyCalendarThree {
2 map<int, int> mp;
4 public:
5 int book(int startTime, int endTime) {
6 mp[startTime]++;
7 mp[endTime]--;
9 int res = 0, acc = 0;
10 for (const auto [_, add] : mp) {
11 res = max(res, acc += add);
12 }
14 return res;
15 }
16 };
18 // Vector solution
19 class MyCalendarThree {
20 using type_t = pair<int, int>;
21 vector<type_t> vec;
23 public:
24 int book(int startTime, int endTime) {
25 const type_t start = {startTime, 1};
26 const type_t end = {endTime, -1};
28 // trick to insert into a sorted vector
29 vec.insert(upper_bound(vec.begin(), vec.end(), start), start);
31 vec.insert(upper_bound(vec.begin(), vec.end(), end), end);
33 int res = 0, acc = 0;
34 for (const auto [_, add] : vec) {
35 res = max(res, acc += add);
36 }
38 return res;
39 }
40 };