Greedy Algorithms8 sections · 316 units
Open in Course

Meeting Rooms II - Implementation

The code

function minMeetingRooms(intervals)
 if length of intervals = 0 then
 return 0
 sort intervals by start time ascending
 heap := min-heap containing intervals[0].end
 for i from 1 to length of intervals - 1
 if intervals[i].start >= heap.min() then
 heap.pop()
 heap.push(intervals[i].end)
 return heap.size()

Time: O(nlogn)O(n \log n). Space: O(n)O(n) for heap.