Blind 75Easy

Meeting Rooms — Sort and Check Overlaps

Sort intervals by start time and ensure each meeting starts after the previous one ends.

IntervalSorting

Problem Statement

Determine if a person could attend all meetings given start-end intervals.

Why This Problem Matters

  • Simpler precursor to Meeting Rooms II.
  • Tests interval sorting and overlap detection.
  • Appears frequently as a warm-up in interviews.

Thought Process

Sort by start time

Ordering makes it easy to compare each meeting against the previous one.

Check for overlaps

If current.start < previous.end, meetings conflict.

Return true when all non-overlapping

After loop, if no conflicts found, all meetings are attendable.

Step-by-Step Reasoning

  1. Sort intervals by start.
  2. Iterate i from 1..n-1; if intervals[i][0] < intervals[i-1][1], return false.
  3. Return true after loop completes.

Dry Run

[[0,30],[5,10],[15,20]]

Overlap detected since 5 < 30; return false.

[[7,10],[2,4]]

No overlaps; return true.

Complexity Analysis

Time

O(n log n)

Space

O(1)

Why

Sorting dominates; comparisons are linear.

Annotated Solution

JAVA
import java.util.Arrays;

public class MeetingRooms {
  public boolean canAttendMeetings(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
    for (int i = 1; i < intervals.length; i += 1) {
      if (intervals[i][0] < intervals[i - 1][1]) {
        return false;
      }
    }
    return true;
  }
}

Sorting ensures only adjacent intervals need comparison, keeping logic simple.

Common Pitfalls

  • Skipping sort leads to incorrect comparisons.
  • Using <= instead of < treats back-to-back meetings as conflict; confirm requirement with interviewer.
  • Returning after first non-overlap misses later conflicts—ensure loop continues.

Follow-Up Questions

  • How would you return the maximum number of meetings that can be attended? (Interval scheduling.)
  • Can you handle meeting priorities or weights?

Key Takeaways

Sorting is often the first step in interval problems.
Clarify whether touching meetings conflict to choose < vs <=.
Meeting Rooms II builds directly on this logic—be ready to discuss.