Fork me on GitHub

Meeting Rooms II

Description

https://leetcode.com/problems/meeting-rooms-ii/description/

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 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) {
int size = intervals.size();
if (size == 1) return 1;

map<int, int> timer;
for (auto& interval : intervals) {
++timer[interval.start];
--timer[interval.end];
}
int currentHouse = 0, peak = 0;
for (auto& item : timer) {
currentHouse += item.second;
peak = max(currentHouse, peak);
}
return peak;
}
};