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.

Solution

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        if(intervals.length == 0) return 0;
        // find the most overlap
        Arrays.sort(intervals, new Comparator<Interval>(){
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.start, i2.start);
            }
        });

        int cnt = 0;
        int max = 0;
        Queue<Interval> queue = new PriorityQueue<Interval>(new Comparator<Interval>() {
            public int compare(Interval i1, Interval i2) {
                return Integer.compare(i1.end, i2.end);
            }

        });

        for(int i = 0; i < intervals.length; i++) {
            while(!queue.isEmpty() && intervals[i].start >= queue.peek().end) {
                queue.poll();
                cnt--;
            }
            queue.offer(intervals[i]);
            cnt++;
            max = Integer.max(max, cnt);
        }
        return max;
    }
}

results matching ""

    No results matching ""