Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
The heap solution is unnecessarily complicated

michaeljroytman

Jan 23, 2026

Why do we need a heap? Am I missing something?

If we sort the meetings by start time (and end time if the start times are the same), then we can simply use sliding window to find the longest length of overlapping meetings.

When the meetings do not overlap, we move the left pointer. When they do, we move the right.

Runtime complexity is O(n logn) because of sorting.

Space complexity is O(1).

/*class Meeting { constructor(start, end) { this.start = start; this.end = end; } }*/ class Solution { findMinimumMeetingRooms(meetings) { let minRooms = 0; /** * Sort the meetings by start time. * Then, apply sliding window. * * When the meetings overlap, grow the window. when they do not, shrink it. * * The maximum length of the window is the number of rooms we need. */ meetings.sort((a,b) => { const start = a.start - b.start; if (start === 0) { return a.end - b.end; } else { return start; } }); let windowRight = 0; let windowLeft = 0; while (windowRight < meetings.length) { const meetingRight = meetings[windowRight]; let meetingLeft = meetings[windowLeft]; while (!this.overlaps(meetingLeft, meetingRight)) { meetingLeft = meetings[++windowLeft]; } minRooms = Math.max(minRooms, windowRight - windowLeft + 1); windowRight += 1; } return minRooms; } overlaps(a,b) { return a.end > b.start; } }

0

0

Comments
M
michaeljroytman a month ago

I was missing something.

The above submission does not handle the case [[1,10],[2,3],[4,5]] correctly.

This test case only needs 2 rooms because [1,10] overlaps with [2,3] and [4,5] but [2,3]and [4,5] do not overlap with each other.