AlgoMaster Logo

My Calendar II

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force with Double Booking Check

Intuition:

The brute force approach involves checking if a new event can be added without causing a double booking. This requires maintaining two lists: one for all booked events and another for overlaps of these events. Every new event is checked against these lists, ensuring it doesn't add a triple booking.

Code:

2. Using ArrayList Overlap and Bookings

Intuition:

An optimization over the previous approach. Instead of blindly checking and updating as in brute force, we focus on preventing any triple bookings by carefully maintaining only necessary overlaps.

Code:

3. TreeMap for Optimized Time Complexity

Intuition:

Using a TreeMap allows us to efficiently track the number of active bookings at any point in time. By incrementing at the start of a booking and decrementing at the end, we can determine double bookings through a sweep-line algorithm.

Code: