【LeetCode】253. Meeting Rooms II 解題報告(C++)
- 作者: 負雪明燭
- id: fuxuemingzhu
- 個人部落格:http://fuxuemingzhu.cn/
題目地址:https://leetcode-cn.com/problems/meeting-rooms/
題目描述
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
題目大意
給定一個會議時間安排的陣列,每個會議時間都會包括開始和結束的時間 [[s1,e1],[s2,e2],…] (si < ei),為避免會議衝突,同時要考慮充分利用會議室資源,請你計算至少需要多少間會議室,才能滿足這些會議安排。
解題方法
排序+堆
這個做法參考了官方解答,先對所有的區間按照開始時間進行排序,使用小根堆儲存每個會議的結束時間,堆的大小表示了現在已經佔用了多少個會議室。
思路是,我們要申請會議室的時候,先看是不是可以釋放會議室,將已經佔用的會議室釋放。
遍歷判斷每個區間的開始時間是否大於等於小根堆中最小元素(最早結束時間),如果大於等於,說明堆裡面有個會議要結束了,將其彈出,代表釋放了一個會議室;否則,說明堆裡面的已經在使用的會議室沒有空閒,只能新增加一個會議室。
由於優先佔用釋放的會議室,所以最後堆裡面的元素個數表示總的需要佔用多少個會議室。
C++程式碼如下:
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>&b) {return a[0] < b[0];});
priority_queue<int, vector<int>, greater<int>> que;
for (vector<int>& interval : intervals) {
if (!que.empty() && interval[0] >= que.top()) {
que.pop();
}
que.push(interval[1]);
}
return que.size();
}
};
參考資料:https://leetcode-cn.com/problems/meeting-rooms-ii/solution/hui-yi-shi-ii-by-leetcode/
日期
2019 年 9 月 17 日 —— 聽了hulu宣講會,覺得hulu的壓力不大
相關文章
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- [Leetcode]253. Meeting Rooms IILeetCodeOOM
- 253. Meeting Rooms IIOOM
- LeetCode 253. Meeting Rooms II(會議室)LeetCodeOOM
- LeetCode—253.會議室 II(Meeting Rooms II)——分析及程式碼(C++)LeetCodeOOMC++
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- LeetCode-Meeting Rooms IILeetCodeOOM
- [LeetCode 253] Meeting Rooms IILeetCodeOOM
- *LeetCode-Meeting Rooms IILeetCodeOOM
- Meeting Rooms IIOOM
- [LeetCode253]Meeting Rooms IILeetCodeOOM
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- LeetCode 題解(254) : Meeting RoomsLeetCodeOOM
- Leetcode 253:Meeting Rooms II(超詳細的解法!!!)LeetCodeOOM
- Leetcode: Meeting RoomsLeetCodeOOM
- LeetCode #252 - Meeting RoomsLeetCodeOOM
- Leetcode 252 Meeting RoomsLeetCodeOOM
- [LeetCode 252] Meeting RoomsLeetCodeOOM
- LeetCode252 Meeting RoomsLeetCodeOOM
- leetcode-252-Meeting RoomsLeetCodeOOM
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- [LeetCode] Meeting Rooms 會議室LeetCodeOOM
- Leetcode 252. Meeting Rooms (Easy) (cpp)LeetCodeOOM
- LeetCode 252. Meeting Rooms (Java版; Easy)LeetCodeOOMJava
- LeetCode 252. Meeting Rooms(會議室)LeetCodeOOM
- 252. Meeting RoomsOOM
- Facebook面試題 meeting rooms 求同時最多meeting room的時間面試題OOM
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解題報告LeetCode
- 【LeetCode】416. Partition Equal Subset Sum 解題報告(Python & C++)LeetCodePythonC++
- Leetcode 第136場周賽解題報告LeetCode
- LeetCode Weekly Contest 96 解題報告LeetCode
- Leetcode Weekly Contest 95解題報告LeetCode
- Leetcode Weekly Contest94 解題報告LeetCode
- LeetCode解題報告 279. Perfect Squares [medium]LeetCode
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- LeetCode 解題報告 - 2. Add Two NumbersLeetCode
- LeetCode題解(0407):接雨水II(Python)LeetCodePython