253. Meeting Rooms II

Description

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

Constraints

Approach

Examples

Input: [[0, 30], [5, 10], [15, 20]]

Output: 2

Solutions

/**
 * Time complexity : O(NlogN).
 * Space complexity : O(N)
 */

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

Follow up

Last updated

Was this helpful?