会议室 II

题目:
描述
给定一系列的会议时间间隔intervals,包括起始和结束时间[[s1,e1],[s2,e2],…] (si < ei),找到所需的最小的会议室数量。

样例1
输入: intervals = [(0,30),(5,10),(15,20)]
输出: 2
解释:
需要两个会议室
会议室1:(0,30)
会议室2:(5,10),(15,20)

样例2
输入: intervals = [(2,7)]
输出: 1
解释:
只需要1个会议室就够了

解题思路:先进行排序,然后运用优先队列进行解决

/**
 * Definition of Interval:
 * public classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 * }
 */

public class Solution {
    /**
     * @param intervals: an array of meeting time intervals
     * @return: the minimum number of conference rooms required
     */
    public int minMeetingRooms(List<Interval> intervals) {
        // Write your code here
        if(intervals == null || intervals.size() == 0) {
            return 0;
        }

        Collections.sort(intervals, (a, b) -> {
            if(a.start == b.start) {
                return a.end - b.end;
            }

            return a.start - b.start;
        });

        PriorityQueue<Integer> queue = new PriorityQueue<Integer>((a, b) -> {
            return a - b;
        });

        queue.offer(intervals.get(0).end);
        int len = intervals.size();
        for(int i = 1; i < len; i++) {
            Interval cur = intervals.get(i);
            if(!queue.isEmpty() && cur.start >= queue.peek()) {
                queue.poll();
            }
            queue.offer(cur.end);
        }

        return queue.size();
    }
}