101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 2 main approaches to solve this problem:

  1. Brute Force Approach - Complex approach
  2. Sorting Approach - Complex approach

Approach 1: Brute Force Approach

Let's start by understanding the problem: we need to determine if there are any overlapping meetings in the given intervals.

Thinking Process: The most straightforward approach is to check every pair of meetings to see if they overlap. Two meetings overlap if one starts before the other ends.

For each pair of meetings (i, j), we check if meeting i overlaps with meeting j. If any pair overlaps, we return false.

Intuition: This approach is simple to understand and implement, but it's not the most efficient as it requires checking all pairs of meetings, resulting in O(n²) time complexity.

Algorithm:

  1. For each meeting i:
  2. For each meeting j (where j != i):
  3. Check if meeting i overlaps with meeting j:
  4. If start_i < end_j and start_j < end_i, they overlap
  5. Return false if they overlap
  6. Return true if no overlaps are found

Time Complexity:

O(n²)

We need to compare each meeting with every other meeting, resulting in n * (n-1) / 2 comparisons, which is O(n²).

Space Complexity:

O(1)

We only use a constant amount of extra space regardless of the input size.

Approach 2: Sorting Approach

We can optimize the brute force approach by sorting the meetings by their start times.

Thinking Process: If we sort the meetings by their start times, then we only need to check if each meeting ends before the next one starts. This is because if meeting i ends before meeting i+1 starts, and meeting i+1 ends before meeting i+2 starts, then meeting i also ends before meeting i+2 starts.

After sorting, we iterate through the meetings and check if the current meeting ends before the next one starts. If not, we return false.

Intuition: By sorting the meetings, we reduce the problem to a linear scan, checking only adjacent meetings for overlaps, which is much more efficient than checking all pairs.

Algorithm:

  1. Sort the intervals by their start times
  2. For each meeting i from 0 to n-2:
  3. Check if meeting i ends after meeting i+1 starts:
  4. If end_i > start_{i+1}, they overlap
  5. Return false if they overlap
  6. Return true if no overlaps are found

Time Complexity:

O(n log n)

Sorting the intervals takes O(n log n) time, and then we do a linear scan which takes O(n) time. The overall time complexity is dominated by the sorting step, which is O(n log n).

Space Complexity:

O(1)

We only use a constant amount of extra space regardless of the input size (assuming the sort is in-place).

Pseudocode

solution.pseudo
1
2
3
4
5
6
function canAttendMeetings(intervals):
for i from 0 to intervals.length - 1:
for j from i + 1 to intervals.length - 1:
if intervals[i][0] < intervals[j][1] and intervals[j][0] < intervals[i][1]:
return false
return true
ProblemSolutionCode
101 Logo
onenoughtone