LeetCode--No.253--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

首先要排序。和昨天的題目有點像,類似於數組找interval的問題。
排好序之後,要意識到,接下來要根據前一個會議的結束時間,查找是不是有可用的會議室。

進一步熟悉priorityqueue

 

class Solution {
    public int minMeetingRooms(int[][] intervals) {
        if (intervals.length < 2)  return intervals.length;
        Arrays.sort(intervals, (p1, p2) -> p1[0] - p2[0]);
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int[] interval: intervals){
            if (pq.size() > 0 && interval[0] >= pq.peek()){
                pq.poll();
            }
            pq.add(interval[1]);
        }
        return pq.size();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章