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.
For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.
Hide Company Tags Google Facebook
Hide Tags Heap Greedy Sort
Hide Similar Problems (H) Merge Intervals (E) Meeting Rooms
这题刚开始想着用个stack按照上一个结束时间和当前开始时间的关系in, out。不对。
看了大家的解法。 用map存储所有start end point,并且++mp[start], –mp[end]. 因为map 是ordered的, 最后traverse 所有elements in map 求room += m.second. 这样做就可以知道最大重叠meeting的个数,也就可以求出我们到底最少需要多少meeting room。
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
if(intervals.empty()) return 0;
map<int, int> mp;
for(Interval i : intervals){
++mp[i.start];
--mp[i.end];
}
int room = 0, maxRoom = INT_MIN;
for(auto m : mp){
maxRoom = max(maxRoom, room += m.second);
}
return maxRoom;
}
};