Problem
Submissions

Solution: Meeting Rooms

Statement

Naive approach

The naive approach to solving this problem is to compare each meeting interval with other meetings to check if any two meetings overlap. The meetings overlap with each other if one meeting starts while the other meeting is still in progress. To check this, we have to make sure that the second meeting begins before the first meeting ends. This way, we would get our solution, but the computational cost of achieving this is very high, since we have to compare each meeting with the rest of the meetings. The time complexity of this approach will be O(n2)O(n^2). Let's try to devise an optimized approach to solve this problem.

Optimized approach using merge intervals

An optimized approach to solve this problem is to use the merge intervals technique. In this approach, we will sort the given meeting intervals in ascending order with respect to their start time. After this, we will iterate over these sorted intervals and check if any two consecutive meetings overlap with each other. To do this, we compare the end time of the first meeting with the start time of the second meeting. If the start time is less than the end time, we would simply return FALSE, meaning a person cannot attend all the given meetings. Otherwise, we keep checking all the intervals and return TRUE if no clash occurs between any two meetings.

Let's look at the illustration below to better understand the solution.

Problem
Submissions

Solution: Meeting Rooms

Statement

Naive approach

The naive approach to solving this problem is to compare each meeting interval with other meetings to check if any two meetings overlap. The meetings overlap with each other if one meeting starts while the other meeting is still in progress. To check this, we have to make sure that the second meeting begins before the first meeting ends. This way, we would get our solution, but the computational cost of achieving this is very high, since we have to compare each meeting with the rest of the meetings. The time complexity of this approach will be O(n2)O(n^2). Let's try to devise an optimized approach to solve this problem.

Optimized approach using merge intervals

An optimized approach to solve this problem is to use the merge intervals technique. In this approach, we will sort the given meeting intervals in ascending order with respect to their start time. After this, we will iterate over these sorted intervals and check if any two consecutive meetings overlap with each other. To do this, we compare the end time of the first meeting with the start time of the second meeting. If the start time is less than the end time, we would simply return FALSE, meaning a person cannot attend all the given meetings. Otherwise, we keep checking all the intervals and return TRUE if no clash occurs between any two meetings.

Let's look at the illustration below to better understand the solution.