AlgoMaster Logo

My Calendar I

Ashish

Ashish Pratap Singh

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force Approach

Intuition:

The brute force approach involves storing every booked interval and iterating through the list whenever a new booking request comes in. This approach checks for conflicts by iterating through each previously booked interval and ensuring no overlap.

Code:

2. Optimized Using TreeMap

Intuition:

To reduce the time complexity for checking overlaps, we can use a TreeMap. The TreeMap data structure maintains the sorted order of starts, which allows faster lookups. We can utilize its floor and ceiling functions to efficiently check for overlaps with neighboring intervals.

Code: