oneby7

Range Queries Lazy propagation

614. My Calendar III

Hard

A calendar takes bookings, and every booking is accepted, however many others it overlaps. A booking [startTime, endTime) covers every time t with startTime ≤ t < endTime. The end time itself is not covered, so a booking that ends at 20 and one that starts at 20 do not overlap.

After each booking, report the largest number of bookings that cover the same time, over all the bookings made so far. Write a class MyCalendarThree with a constructor and one method:

  • MyCalendarThree() starts with an empty calendar.
  • book(startTime, endTime) adds the booking [startTime, endTime), then returns the largest number of bookings that cover the same time.

Your class is tested on a new MyCalendarThree, which then gets one call book(bookings[i][0], bookings[i][1]) for each row of bookings, in order. The answer is the list of what each call returns.

In C# and Go the method is Book. Go creates the object with Constructor(), and Rust with MyCalendarThree::new().

Example 1

Input: bookings = [[5, 15], [30, 40], [10, 35], [12, 20], [0, 50], [20, 30]]

Output: [1,1,2,3,4,4]

Explanation: After the fourth booking, the times 12 to 14 lie in [5, 15), [10, 35) and [12, 20), so the answer is 3. [0, 50) covers those times too and makes it 4. The last booking reaches only 3 at its own times, but 4 bookings still meet at time 12, so the answer stays 4.

Example 2

Input: bookings = [[10, 20], [20, 30], [0, 10], [15, 25]]

Output: [1,1,1,2]

Explanation: The first three bookings only touch at their ends, so no time lies in two of them. The last booking overlaps [10, 20) and [20, 30), but never both at the same time, so the answer is 2.

Example 3

Input: bookings = [[0, 1000000000], [0, 1000000000], [999999999, 1000000000]]

Output: [1,2,3]

Explanation: The same booking can be made twice, and both count. Time 999999999 lies in all three bookings.

Check your understanding

Work out the answer for this input before you write any code.

bookings = [[1, 4], [4, 7], [2, 6]]

Constraints

  • 1 ≤ bookings.length ≤ 105, so book is called at most 105 times.
  • bookings[i].length == 2
  • 0 ≤ startTime < endTime ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.