-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path919.meeting-rooms-ii.cpp
69 lines (65 loc) · 1.68 KB
/
919.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
// Tag: Heap, Greedy, Sweep Line, Sort
// Time: O(NlogN)
// Space: O(N)
// Ref: Leetcode-253
// Note: -
// Given an array of meeting time intervals consisting of start and end times `[[s1,e1],[s2,e2],...] (si < ei)`, find the minimum number of conference rooms required.
//
// **Example1**
//
// ```
// Input: intervals = [(0,30),(5,10),(15,20)]
// Output: 2
// Explanation:
// We need two meeting rooms
// room1: (0,30)
// room2: (5,10),(15,20)
// ```
//
// **Example2**
//
// ```
// Input: intervals = [(2,7)]
// Output: 1
// Explanation:
// Only need one meeting room
// ```
//
// (0,8),(8,10) is not conflict at 8
/**
* Definition of Interval:
* class Interval {
* public:
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
* }
*/
class Solution {
public:
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
int minMeetingRooms(vector<Interval> &intervals) {
// Write your code here
vector<pair<int, int>> meetings;
for (auto x: intervals) {
meetings.emplace_back(x.start, 1);
meetings.emplace_back(x.end, -1);
}
int count = 0;
int res = 0;
int n = meetings.size();
sort(meetings.begin(), meetings.end());
for (int i = 0; i < n; i++) {
count += meetings[i].second;
if (i == n - 1 || meetings[i].first != meetings[i + 1].first) { // second -1 comes first after sorting, this is not necessary
res = max(res, count);
}
}
return res;
}
};