LeetCode – Meeting Rooms II (Java)

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] find the minimum number of conference rooms required.

Java Solution
When a room is taken, the room can not be used for anther meeting until the current meeting is over. As soon as the current meeting is finished, the room can be used for another meeting. We can sort the meetings by start timestamps and sequentially assign each meeting to a room. Each time when we assign a room for a meeting, we check if any meeting is finished so that the room can be reused. In order to efficiently track the earliest ending meeting, we can use a min heap. Whenever an old meeting ends before a new meeting starts, we reuse the room (i.e., do not add more room). Otherwise, we need an extra room (i.e., add a room).

The time complexity is O(N*log(N)).

public int minMeetingRooms(int[][] intervals) {
    Arrays.sort(intervals, Comparator.comparing((int[] itv) -> itv[0]));
 
    PriorityQueue<Integer> heap = new PriorityQueue<>();
    int count = 0;
    for (int[] itv : intervals) {
        if (heap.isEmpty()) {
            count++;
            heap.offer(itv[1]);
        } else {
            if (itv[0] >= heap.peek()) {
                heap.poll();
            } else {
                count++;
            }
 
            heap.offer(itv[1]);
        }
    }
 
    return count;
}

There was a discussion in the comments about why a regular queue is not good enough. I draw an example below to show why sorting based on start time and using a priority queue is necessary.

Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Cat Racket

    A JavaScript Solution:
    Chinese: https://youtu.be/uEGNNeiMxuI
    English: https://youtu.be/24li7yc91us
    Youtube Channel: youtube.com/c/CatRacketCode
    Facebook: https://www.facebook.com/groups/2094071194216385/

  • Pooja Gupta

    what about 0-2,0-4,0-5,7-8
    It is showing extra count
    answer should be 3 instead it is 4

  • Prashant Nigam

    What would be the runtime (big O) of your approach?

  • Prashant Nigam

    You don’t require a counter, heap size at the end is the required number of rooms

  • It is fine. Output is one meeting room.

  • santosh sunny

    Hi , i got a doubt. Whenever we are taking a new interval, instead of comparing with the smallest ending times why can’t we start comparing from largest of the finished ending times until we get a ending time which is smaller than current interval start time .
    Example : priority Queue : {13,16}
    Current interval : [17,21]
    If we choose 16 -> priority queue :{16,17}
    So here instead of 13 if we choose 16 -> priority queue : {13,17}
    Isn’t the second case better than first one as it will give less collisions compared to first one.

  • Bonsai

    I don’t think your solution is right. Try this example [[7,10],[2,4]]

  • Satish


    public static int meetingRooms(Interval[] intervals) {
    if (intervals.length == 0) {
    return 0;
    }
    // Sort start of interval in ascending order
    Arrays.sort(intervals, new Comparator() {
    public int compare(Interval o1, Interval o2) {
    return o1.start - o2.start;
    }
    });
    int result = intervals.length; // Worst case all room will need its own room
    for (int i = 0; i = intervals[i + 1].start // curr overlap with next
    || (intervals[i].start >= intervals[i + 1].start && intervals[i].end <= intervals[i + 1].end) // curr completely overlap with next
    ) {
    result--;
    }
    }
    return result;
    }

  • Amit

    why wont it?
    It will work.

    First 10-16 is compared with 11-13… count is incremented
    then 11-13 is added to priority queue..
    then 11-13 is compared with 14-17… count is NOT incremented.

    Result: 2

  • rohit

    this will not work for 10-16,11-13,14-17

  • Ankit Shah
  • Ankur

    Instead of Priority Queue take simple queue to store elements but just sort by endTime .


    private static int minMeetingRoomsQueue(Interval[] inte) {
    int c = 0;
    Arrays.sort(inte,new Comparator(){

    @Override
    public int compare(Interval o1, Interval o2) {
    return o1.eTime - o2.eTime;
    }

    });
    Queue q = new LinkedList();
    q.add(inte[0].eTime);
    c++;
    for(int i=1;i<inte.length;i++){
    if(inte[i].sTime < q.peek()){
    c++;
    }else{
    q.poll();
    }
    q.add(inte[i].eTime);
    }
    return c;
    }