Intermediate Level

Implement the minimumMeetingRoomsHeap method that returns the minimum number of meeting rooms required for all intervals.

The input intervals contains size meeting time intervals. Each interval has a start time and an end time.

Your task is to return the minimum number of meeting rooms required so that all meetings can be scheduled without overlap in the same room.

For example, [[0,30],[5,10],[15,20]] needs 2 rooms because the meeting from 0 to 30 overlaps with the other two meetings.

Example 1
Input:
intervals (int[][]) = [[0,30],[5,10],[15,20]]
size (int) = 3
Return:
(int) 2
Example 2
Input:
intervals (int[][]) = [[7,10],[2,4]]
size (int) = 2
Return:
(int) 1
Example 3
Input:
intervals (int[][]) = [[1,5],[2,6],[3,7]]
size (int) = 3
Return:
(int) 3

Sort all meetings by start time and use a min heap to track room ending times.

The heap top gives the earliest time at which any currently used room becomes free. When a new meeting starts, compare its start time with this earliest end time.

If the earliest room has already become free, remove it and reuse that room. Then insert the current meeting end time. The largest heap size during this process represents the required room count, and after processing all meetings the heap size also gives the minimum room count.

Pseudocode:

function minimumMeetingRoomsHeap(intervals, size):
    if size == 0:
        return 0
    sort intervals by start time
    create empty minHeap
    for each meeting in intervals:
        start = meeting[0]
        end = meeting[1]
        if heap is not empty and top of heap <= start:
            remove top from heap
        push end into heap
    return heap size
Run your code to see the result.