Skip to content

Practice My Calendar II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#731
LeetCode Problem

My Calendar II

Problem Statement

MyCalendarTwo.book(startTime,endTime) adds a half-open event only if no time would become covered by three events. Return true on acceptance or false without mutation on rejection; double overlaps are allowed. Practice approach: Keep accepted events and their double-covered intersections. Reject a new event if it overlaps any double-covered interval. Otherwise add intersections with existing events and store the event. Alternatively, use an endpoint sweep and roll back tentative changes on rejection. Test identical intervals and touching endpoints.

Source: My Calendar II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["MyCalendarTwo","book","book","book"]
[[],[4,8],[6,10],[7,9]]
Output
[null,true,true,false]
Explanation

Half-open endpoints and overlap counts determine each result; rejected events do not remain in the calendar.

Constraints

0 <= startTime < endTime <= 10^9
At most 1000 book calls.

Related LeetCode Problems

Practice more problems similar to My Calendar II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.