问题描述
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
题目链接:
思路分析
给一个存储了会议开始时间和结束时间的数组,问这些会议至少需要几个会议室才能正常召开。
We use a map to store the number of meeting rooms needed. First iteration of vector intervals stores number of meeting rooms needed in a specific time. When a meeting starts, the map’s value add one. When a meeting ends, the map’s value minus one.
Then in the second iteration, we iterate the map, the initial value of rooms is zero. In a specific time point, just simply add the map’s value. If at that time one or multiple new meetings start, rooms number would add the same number. Then comparing this value with the max rooms number, we could get the final result.
代码
java
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public int minMeetingRooms(Interval[] intervals) {
Map<Integer, Integer> map = new TreeMap<>();
for (Interval in : intervals){
map.put(in.start, map.getOrDefault(in.start, 0) + 1);
map.put(in.end, map.getOrDefault(in.end, 0) - 1);
}
int res = 0, rooms = 0;
for (Integer time : map.keySet()){
rooms += map.get(time);
res = Math.max(res, rooms);
}
return res;
}
}
C++
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
map<int, int> roomChanges;
for (auto i : intervals){
roomChanges[i.start] += 1;
roomChanges[i.end] -= 1;
}
int rooms = 0, maxRoom = 0;
for (auto change : roomChanges){
rooms += change.second;
maxRoom = max(maxRoom, rooms);
}
return maxRoom;
}
};
时间复杂度:O(n)
空间复杂度:O(n)
反思
很巧妙的把握住了整体的思想,不计较每次的变化。一定要使用TreeMap,因为hashmap是使用hash值的,遍历的时候key是没有顺序的;Treemap遍历时则会按照自然顺序排序。
HashMap 与 TreeMap的区别
Another approach. Sort the start time and end time. Then use a pointer j to denotes the last ended meeting’s end time.
Then start from the second element in start, if start after or equal end[j], it means this meeting could have in current count rooms, and j should < I for you can’t start a meeting it does not start yet. If start[I] < end[j], it means we need more rooms.
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution {
public int minMeetingRooms(Interval[] intervals) {
if (intervals == null || intervals.length == 0){
return 0;
}
int n = intervals.length;
int[] start = new int[n];
int[] end = new int[n];
for (int i = 0; i < n; i++){
start[i] = intervals[i].start;
end[i] = intervals[i].end;
}
Arrays.sort(start);
Arrays.sort(end);
int count = 1;
for (int i = 1, j = 0; i < n; i++){
if (j < i && start[i] >= end[j]){
j++;
}
else if( start[i] < end[j]){
count++;
}
}
return count;
}
}