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 [startTime, endTime), 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 startTime, int endTime) Returns an integer k representing the largest integer such that there exists a k-booking in the calendar.

Input & Output

Example 1 — Basic Booking Sequence
$ Input: operations = ["MyCalendarThree","book","book","book"], values = [[],[10,20],[15,25],[20,30]]
Output: [1,2,2]
💡 Note: First book(10,20) returns 1. Second book(15,25) overlaps with first, max k-booking is 2. Third book(20,30) doesn't increase maximum overlap, still 2.
Example 2 — Multiple Overlaps
$ Input: operations = ["MyCalendarThree","book","book","book","book"], values = [[],[10,40],[20,30],[25,35],[5,15]]
Output: [1,2,3,3]
💡 Note: Progressive overlap: first booking alone (1), second overlaps with first (2), third overlaps with both first and second at time 25-30 (3), fourth doesn't increase maximum.

Constraints

  • 0 ≤ start < end ≤ 109
  • At most 400 calls will be made to book

Visualization

Tap to expand
My Calendar III: Track Maximum Overlapping EventsInput: Booking Operationsbook(10, 20)book(15, 25)book(20, 30)Timeline Visualization:[10,20)[15,25)[20,30)1015202530Maximum Overlap Count:k = 2(at time 15-20)Output: [1, 2, 2]
Understanding the Visualization
1
Input
Sequence of booking operations with [start, end) intervals
2
Process
Track maximum number of overlapping intervals at any time
3
Output
Return k-booking count after each operation
Key Takeaway
🎯 Key Insight: Use difference array to efficiently track booking count changes at start/end times
Asked in
Google 25 Amazon 18 Microsoft 15
28.0K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen