Meeting Schedule - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We are given a list of meeting time intervals, where each interval is of the form [start, end]. We need to determine whether it is possible to attend all of the meetings without any overlap. In other words, we return true if no two meetings overlap in time, and false otherwise.

The key idea is that by sorting all meetings by their start time, we can reason incrementally as we move from left to right. At any point in the scan, all meetings we’ve processed so far are already conflict-free β€” meaning each one ends before (or exactly when) the next one starts.

Because of this property, when checking a current meeting, it’s enough to compare it only with the previous meeting in the sorted order. If the current meeting starts before the previous one ends, there’s an overlap and it’s impossible to attend all. Otherwise, the schedule remains conflict-free, and we continue.

High-Level Strategy

  1. Step 1 – Sort meetings by start time.
    Sort all intervals in ascending order of their start time so meetings are in the order they begin. Sorting enables us to determine overlaps efficiently β€” it guarantees that to check for conflicts, we only need to compare each meeting with the previous one in the sorted order.
  2. Step 2 – Scan and check for overlap.
    Starting from the second meeting, compare each current meeting with the previous meeting:
    • If current.start < previous.end, the current meeting starts before the previous one ends β†’ overlap β†’ return false.
    • Otherwise (current.start β‰₯ previous.end), there is no overlap, and we continue checking the next meeting.
  3. Step 3 – Return the result.
    If we finish scanning without finding any overlap, all meetings can be attended. Return true.

Why This Works

  • Sorting creates a reliable order:
    Sorting by start time enables us to detect overlaps efficiently β€” it guarantees that to find any conflict, we only need to compare each meeting with the previous one in the sorted order. This makes it possible to reason incrementally as we move through the list.
  • Early exit:
    As soon as an overlap is detected, we can immediately return false, because attending all meetings becomes impossible.

Time Complexity

Sorting the meetings by start time takes O(N log N), where N is the number of intervals. The single left-to-right scan takes O(N), giving an overall complexity of O(N log N).

Space Complexity

The algorithm itself uses only a few scalar variables while scanning, resulting in O(1) extra space.
The overall space complexity is determined by the sorting step, which depends on the underlying implementation β€” typically one of O(1), O(log N), or O(N), where N is the number of meetings.

Visucodize editorial solution titled JS Solution for NeetCode's Meeting Schedule coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://neetcode.io/problems/meeting-schedule.