[LeetCode] 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

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

 

這道題是之前那道 Meeting Rooms 的拓展,那道題只問我們是否能參加所有的會,也就是看會議之間有沒有時間沖突,而這道題讓求最少需要安排幾個會議室,有時間沖突的肯定需要安排在不同的會議室。這道題有好幾種解法,先來看使用 TreeMap 來做的,遍歷時間區間,對於起始時間,映射值自增1,對於結束時間,映射值自減1,然后定義結果變量 res,和房間數 rooms,遍歷 TreeMap,時間從小到大,房間數每次加上映射值,然后更新結果 res,遇到起始時間,映射是正數,則房間數會增加,如果一個時間是一個會議的結束時間,也是另一個會議的開始時間,則映射值先減后加仍為0,並不用分配新的房間,而結束時間的映射值為負數更不會增加房間數,利用這種思路可以寫出代碼如下:

 

解法一:

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        map<int, int> m;
        for (auto a : intervals) {
            ++m[a[0]];
            --m[a[1]];
        }
        int rooms = 0, res = 0;
        for (auto it : m) {
            res = max(res, rooms += it.second);
        }
        return res;
    }
};

 

第二種方法是用兩個一維數組來做,分別保存起始時間和結束時間,然后各自排個序,定義結果變量 res 和結束時間指針 endpos,然后開始遍歷,如果當前起始時間小於結束時間指針的時間,則結果自增1,反之結束時間指針自增1,這樣可以找出重疊的時間段,從而安排新的會議室,參見代碼如下:

 

解法二:

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        vector<int> starts, ends;
        int res = 0, endpos = 0;
        for (auto a : intervals) {
            starts.push_back(a[0]);
            ends.push_back(a[1]);
        }
        sort(starts.begin(), starts.end());
        sort(ends.begin(), ends.end());
        for (int i = 0; i < intervals.size(); ++i) {
            if (starts[i] < ends[endpos]) ++res;
            else ++endpos;
        }
        return res;
    }
};

 

再來一看一種使用最小堆來解題的方法,這種方法先把所有的時間區間按照起始時間排序,然后新建一個最小堆,開始遍歷時間區間,如果堆不為空,且首元素小於等於當前區間的起始時間,去掉堆中的首元素,把當前區間的結束時間壓入堆,由於最小堆是小的在前面,那么假如首元素小於等於起始時間,說明上一個會議已經結束,可以用該會議室開始下一個會議了,所以不用分配新的會議室,遍歷完成后堆中元素的個數即為需要的會議室的個數,參見代碼如下;

 

解法三:

class Solution {
public:
    int minMeetingRooms(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){ return a[0] < b[0]; });
        priority_queue<int, vector<int>, greater<int>> q;
        for (auto interval : intervals) {
            if (!q.empty() && q.top() <= interval[0]) q.pop();
            q.push(interval[1]);
        }
        return q.size();
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/253

 

類似題目:

Merge Intervals

Meeting Rooms

 

參考資料:

https://leetcode.com/problems/meeting-rooms-ii/

https://leetcode.com/problems/meeting-rooms-ii/discuss/67857/AC-Java-solution-using-min-heap

https://leetcode.com/problems/meeting-rooms-ii/discuss/67883/Super-Easy-Java-Solution-Beats-98.8

https://leetcode.com/problems/meeting-rooms-ii/discuss/67996/C%2B%2B-O(n-log-n)-584%2B-ms-3-solutions

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM