-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path253. Meeting Rooms II.cpp
71 lines (69 loc) · 1.88 KB
/
253. 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
// Solution 1.
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
if(intervals.size() == 0) return 0;
sort(intervals.begin(), intervals.end(), [](Interval a, Interval b){ return a.start < b.start;});
priority_queue<int, vector<int>, greater<int>>pq;
pq.push(intervals[0].end);
int room = 1;
for(int i = 1; i < intervals.size(); i++){
if(intervals[i].start < pq.top()) room++;
else pq.pop();
pq.push(intervals[i].end);
}
return room;
}
};
// Solution 2.
class Solution {
public:
int minMeetingRooms(vector<Interval>& intervals) {
map<int, int>m;
int room = 0, maxRoom = 0;
for(auto x: intervals) m[x.start]++, m[x.end]--;
for(auto x: m) room += x.second, maxRoom = max(maxRoom, room);
return maxRoom;
}
};
// Solution 3.
/**
* 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 res = 0;
sort(intervals.begin(), intervals.end(), [](Interval& a, Interval& b) {
return a.start < b.start;
});
auto comp = [](Interval& a, Interval& b) {
return a.end > b.end;
};
priority_queue<Interval, vector<Interval>, decltype(comp)>pq(comp);
for (auto& i: intervals) {
if (pq.empty() || pq.top().end > i.start) {
++res;
} else {
pq.pop();
}
pq.push(i);
}
return res;
}
};