forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeeting_Rooms_II.cpp
33 lines (33 loc) · 911 Bytes
/
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
/**
* 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 {
static bool compare(Interval const& a, Interval const& b) {
return a.start < b.start;
}
priority_queue<int, vector<int>, greater<int>> Q;
public:
int minMeetingRooms(vector<Interval>& intervals) {
if(intervals.empty()) return 0;
sort(intervals.begin(), intervals.end(), compare);
int rooms = 0;
Q.push(intervals[0].end);
rooms++;
for(int i = 1; i < intervals.size(); ++i) {
if(intervals[i].start < Q.top()) {
++rooms;
Q.push(intervals[i].end);
} else {
Q.pop();
Q.push(intervals[i].end);
}
}
return rooms;
}
};