forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
67 lines (50 loc) · 1.34 KB
/
main2.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
/// Source : https://leetcode.com/problems/meeting-rooms-ii/description/
/// Author : liuyubobobo
/// Time : 2018-09-10
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/// Simulation
/// Using Priority Queue
///
/// Time Complexity: O(nlogn)
/// Space Complexity: 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 {
private:
class CompareInterval{
public:
bool operator()(const Interval& a, const Interval& b){
return a.end > b.end;
}
};
public:
int minMeetingRooms(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), cmpIntervals);
priority_queue<Interval, vector<Interval>, CompareInterval> rooms;
int res = 0;
for(const Interval& meeting: intervals){
while(!rooms.empty() && rooms.top().end <= meeting.start)
rooms.pop();
rooms.push(meeting);
res = max(res, (int)rooms.size());
}
return res;
}
private:
static bool cmpIntervals(const Interval& i1, const Interval& i2){
if(i1.start != i2.start)
return i1.start < i2.start;
return i1.end < i2.end;
}
};
int main() {
return 0;
}