Meeting Rooms II

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.

Example 1:

Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
Example 2:

Input: [[7,10],[2,4]]
Output: 1
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

Solution:

class Solution {
    public int minMeetingRooms(int[][] intervals) {
        if (intervals == null || intervals.length == 0) return 0;
        Arrays.sort(intervals, (a, b) -> { return Integer.compare(a[0], b[0]); });
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> { return Integer.compare(a[1], b[1]); });
        pq.offer(intervals[0]);
        int i = 1;
        while (i < intervals.length) {
            int[] lastInterval = pq.poll();
            int[] curInterval = intervals[i];
            if (curInterval[0] >= lastInterval[1]) {
                lastInterval[1] = Math.max(lastInterval[1], curInterval[1]);
                lastInterval[0] = Math.min(lastInterval[0], curInterval[0]);
            } else {
                pq.offer(curInterval);
            }
            pq.offer(lastInterval);
            i ++;
        }
        return pq.size();
    }
}