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.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.

Hint

Train of Thought

those non-overlapping intervals can be assembeled in the same room 

Code

public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if(intervals == null || intervals.length == 0) {
            return 0;
        }

        Arrays.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval v1, Interval v2) {
                return v1.start - v2.start;
            }
        });

        PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
        pq.offer(intervals[0].end);
        int count = 1;

        for(int i = 1; i < intervals.length; i++) {
           //overlapping
            if(pq.peek() > intervals[i].start) {
                count++;
            }
            //not overlapping
            else {
                pq.poll();
            }
            pq.offer(intervals[i].end);
        }

        return count;
    }
}

Complexity

results matching ""

    No results matching ""