Back to DSA

Meeting Rooms II

medium
Acceptance: 45%
IntervalsSortingHeap

Given a list of meeting time intervals [start, end], determine the fewest meeting rooms needed so that no two overlapping meetings share a room.

Examples

Example 1:
Input:intervals = [[0,25],[5,15],[10,20]]
Output:2
Example 2:
Input:intervals = [[3,8],[9,14]]
Output:1

Hints

00:00
import java.util.*;

class Solution {
    public int minMeetingRooms(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int[] i : intervals) {
            if (!pq.isEmpty() && pq.peek() <= i[0]) pq.poll();
            pq.offer(i[1]);
        }
        return pq.size();
    }
}
Time complexityO(n log n)
Space complexityO(n)