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

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

class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
vector<int> starts, ends;
int res = 0, endpos = 0;
for (auto a : intervals) {
starts.push_back(a.start);
ends.push_back(a.end);
}
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<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b){return a.start < b.start;});
priority_queue<int, vector<int>, greater<int>> q;
for (auto a : intervals) {
if (!q.empty() && q.top() <= a.start) q.pop();
q.push(a.end);
}
return q.size();
}
};

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 题目讲解汇总(持续更新中...)

posted @ 2016-03-05 13:58 Grandyang 阅读(...) 评论(...) 编辑 收藏