Editorial solutions and visualizations are prepared with care, but we do not guarantee 100% accuracy or completeness. Please use your own judgment to verify results. Visucodize is not responsible for decisions made based on this content.
Solution code
constCURRENT='current',CURRENT_COLOR='blue',PREV_COLOR='gold';functionvisMainFunction(inputIntervals){initClassProperties();colorSchemaAndExplanationNotification();startPartialBatch();const intervals =newVisArray(...inputIntervals).options({labelExtractionPolicy:JSON.stringify });notification(`
π <b>Step 1: Sort all meetings by start time.</b><br>
We sort so that meetings are in the order they begin. Sorting ensures we can apply the following logic:<br><br>
As we scan from left to right, each meeting weβve processed so far has not caused a conflict β
meaning every earlier meeting already finishes before or when the next one starts.<br><br>
Because of that, when we check the ${makeColoredSpan('current meeting',CURRENT_COLOR)},
itβs enough to compare it only with the ${makeColoredSpan('previous meeting',PREV_COLOR)}:<br>
β’ If the current meeting starts <b>before</b> the previous one ends β overlap β impossible to attend all.<br>
β’ Otherwise, all meetings up to this point remain conflict-free, and we continue.
`);startBatch();
intervals.sort((a, b)=> a[0]- b[0]);endBatch();let index =1;makeVisVariable(index).registerAsIndexPointer(intervals,CURRENT);notification(`
π <b>Step 2:</b> β Now that meetings are sorted in the order they begin,
weβll iterate through them to check for overlaps.<br>
Since the list is sorted, we only need to compare each
${makeColoredSpan('current meeting',CURRENT_COLOR)} with the
${makeColoredSpan('previous meeting',PREV_COLOR)} to ensure thereβs no conflict.
`);endBatch();while(index < intervals.length){startPartialBatch();const prevMeeting = intervals[index -1];const currentMeeting = intervals[index];const svgContent =generateIntervalsComparisonContent(prevMeeting, currentMeeting);if(currentMeeting[0]< prevMeeting[1]){notification(`
β <b>Overlap detected!</b><br>
The ${makeColoredSpan('current meeting',CURRENT_COLOR)}${JSON.stringify(currentMeeting)}
starts <b>before</b> the ${makeColoredSpan('previous meeting',PREV_COLOR)} ends (which ends at ${prevMeeting[1]}).<br><br>
This means these two meetings overlap β you would need to be in both at once, which is impossible.<br>
π¨ Returning <b>false</b>: not all meetings can be attended.<br><br>
${svgContent}`);returnfalse;}else{notification(`
β <b>No overlap detected.</b><br>
The ${makeColoredSpan('current meeting',CURRENT_COLOR)}${JSON.stringify(currentMeeting)}
starts <b>after</b> the ${makeColoredSpan('previous meeting',PREV_COLOR)} ends (which ends at ${prevMeeting[1]}).<br><br>
This means the two meetings do not conflict β we can attend both.<br>
Continuing to check the next meeting.<br><br>
${svgContent}`);}
index +=1;endBatch();}notification(`π <b>Final Result:</b> β Can attend all meetings. Returning <b>true</b>`);returntrue;}functiongenerateIntervalsComparisonContent(prevMeeting, currentMeeting){returngenerateIntervalsSvgContent([[[...prevMeeting,{text:"Previous Meeting",color:PREV_COLOR}]],[[...currentMeeting,{text:"Current Meeting",color:CURRENT_COLOR}]]]);}functioncolorSchemaAndExplanationNotification(){explanationNotification();notification(`
π¨ <b>Color Schema:</b><br>
To make the reasoning visually clear during scanning and comparison:
<ul>
<li><b style="color:${CURRENT_COLOR};">Current Meeting (${CURRENT_COLOR})</b> β
the meeting currently being checked in the iteration.</li>
<li><b style="color:${PREV_COLOR};">Previous Meeting (${PREV_COLOR})</b> β
the last meeting that was processed and confirmed to fit without conflict so far.</li>
</ul>
These colors help distinguish between the two intervals being compared at each step of the algorithm.
`);}functioninitClassProperties(){setClassProperties(CURRENT,{backgroundColor:CURRENT_COLOR});}
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
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.
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.
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.
Input-1
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.