forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
meeting-rooms-ii.cpp
40 lines (37 loc) · 1.02 KB
/
meeting-rooms-ii.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
// Time: O(nlogn)
// Space: O(n)
/**
* 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) {
vector<int> starts, ends;
for (const auto& i : intervals) {
starts.emplace_back(i.start);
ends.emplace_back(i.end);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
int min_rooms = 0, cnt_rooms = 0;
int s = 0, e = 0;
while (s < starts.size()) {
if (starts[s] < ends[e]) {
++cnt_rooms; // Acquire a room.
// Update the min number of rooms.
min_rooms = max(min_rooms, cnt_rooms);
++s;
} else {
--cnt_rooms; // Release a room.
++e;
}
}
return min_rooms;
}
};