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(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()) {
        } else {
            if (itv[0] >= heap.peek()) {
            } else {
    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:
String foo = "bar";
  • 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
    ) {
    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(){

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

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