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
Companies:
Facebook, Google, Microsoft, Amazon, Bloomberg, Uber, Yelp, Apple, Snapchat, Cisco
Related Topics:
Heap, Greedy, Sort
Similar Questions:
// OJ: https://leetcode.com/problems/meeting-rooms-ii/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
vector<int> starts, ends;
for (auto &i : intervals) {
starts.push_back(i.start);
ends.push_back(i.end);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
int ans = 0, cnt = 0;
for (int i = 0, j = 0, N = intervals.size(); i < N;) {
if (starts[i] < ends[j]) {
++cnt;
++i;
ans = max(ans, cnt);
} else if (starts[i] > ends[j]) {
--cnt;
++j;
} else {
++i;
++j;
}
}
return ans;
}
};