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