1,题目要求
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.
给定由开始和结束时间[[s1,e1],[s2,e2],…](si <ei)组成的会议时间间隔数组,找到所需的最小会议室数。
2,题目思路
对于这道题,比较蛋疼的一点是,这道题是被锁住的,必须订阅才能做。然而并没有钱去订阅这个LeetCode,所以,只能从别人的博客了解这道题了。
这道题的意思,是给定一系列会议的开会时间区间,判断一共需要多少个会议室。
简单来说,就是一个活动安排问题。
如果我们手动去操作,会比较简单,主要就是个时间窗安排问题。
而在实现上,我们可以使用map
作为我们的主要容器,并利用其自动排序的特点。在对会议进行遍历时,对开始的时间,我们标记为1,对结束的时间,我们则标记为-1。
然后,从左往右按照时间顺序进行遍历并进行累加即可。
比如对于[0,5]
和[10,15]
,我们在遍历时可以看到,遍历到0,res+1,遍历到5,res则-1,然后遍历到10,res+1,遍历到15,res-1。
我们只需要记录res的最大值即可。
再比如[0,10]
和[2,7]
,我们在遍历时,先0,再2,res变成2,最大值也就变成2。实际上,也是需要2个会议室。
3,代码实现
class Solution {
public:
int minMeetingRooms(vector<Interval> &intervals){
map<int, int> meetingTimeMap;
for(auto &a :intervals){
meetingTimeMap[a.start]++; //开始时间
meetingTimeMap[a.end]--; //结束时间
}
int res = 0, rooms = 0;
for(auto &m : meetingTimeMap){
rooms += m.second;
res = max(res, rooms);
}
return res;
}