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

The basic idea of the solution is that we sequentially assign meeting to a room. We use a min heap to track the earliest ending meeting. Whenever an old meeting ends before a new meeting starts, we remove the old meeting. Otherwise, we need an extra room.

public int minMeetingRooms(Interval[] intervals) {
    if(intervals==null||intervals.length==0){
        return 0;
    }
 
    Comparator<Interval> comp = Comparator.comparing((Interval i)->i.start);
    Arrays.sort(intervals, comp);
 
    PriorityQueue<Integer> queue = new PriorityQueue<>();
    queue.offer(intervals[0].end);
    int count = 1;
    for(int i=1; i<intervals.length; i++){
        int head = queue.peek();
        if(intervals[i].start>=head){
            queue.poll();
        }else{
            count++;
        }
        queue.offer(intervals[i].end);
    }
 
    return count;
}

There was a discussion in the comment 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>
  • 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;
    }