-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0253_minMeetingRooms.cc
More file actions
82 lines (72 loc) · 1.46 KB
/
Problem_0253_minMeetingRooms.cc
File metadata and controls
82 lines (72 loc) · 1.46 KB
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
72
73
74
75
76
77
78
79
80
81
82
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
private:
class Line
{
public:
int start;
int end;
Line() // 兼容vector默认构造
{
start = 0;
end = 0;
}
Line(int s, int e)
{
start = s;
end = e;
}
};
class StartAsc
{
public:
bool operator()(const Line& lhs, const Line& rhs) { return lhs.start < rhs.start; }
};
class EndAsc
{
public:
bool operator()(const Line& lhs, const Line& rhs) { return lhs.end > rhs.end; }
};
public:
int minMeetingRooms(vector<vector<int>>& intervals)
{
vector<Line> lines(intervals.size());
for (int i = 0; i < intervals.size(); i++)
{
lines[i] = {intervals[i][0], intervals[i][1]};
}
std::sort(lines.begin(), lines.end(), StartAsc());
priority_queue<Line, vector<Line>, EndAsc> q;
int max = 0;
for (int i = 0; i < lines.size(); i++)
{
while (!q.empty() && q.top().end <= lines[i].start)
{
q.pop();
}
q.push(lines[i]);
max = std::max(max, (int) q.size());
}
return max;
}
};
void testMinMeetingRooms()
{
Solution s;
vector<vector<int>> n1 = {{0, 30}, {5, 10}, {15, 20}};
vector<vector<int>> n2 = {{7, 10}, {2, 4}};
EXPECT_EQ_INT(2, s.minMeetingRooms(n1));
EXPECT_EQ_INT(1, s.minMeetingRooms(n2));
EXPECT_SUMMARY;
}
int main()
{
testMinMeetingRooms();
return 0;
}