[LeetCode] My Calendar III

732. My Calendar III

A k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events.)

You are given some events [start, end), after each given event, return an integer k representing the maximum k-booking between all the previous events.

Implement the MyCalendarThree class:

  • MyCalendarThree() Initializes the object.
  • int book(int start, int end) Returns an integer k representing the largest integer such that there exists a k-booking in the calendar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyCalendarThree {
map<int, int> m {{-1, 0}};
int res;
public:
MyCalendarThree(): res(0) {}

int book(int start, int end) {
auto st = m.emplace(start, (--m.upper_bound(start))->second);
auto ed = m.emplace(end, (--m.upper_bound(end))->second);
for(auto i = st.first; i != ed.first; i++) {
res = max(res, ++(i->second));
}
return res;
}
};

/**
* Your MyCalendarThree object will be instantiated and called as such:
* MyCalendarThree* obj = new MyCalendarThree();
* int param_1 = obj->book(start,end);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/25/PS/LeetCode/my-calendar-iii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.