-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path253-meeting-rooms-ii.cpp
51 lines (47 loc) · 1.6 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
// Time: O(N log N), Space: O(N)
#include <queue>
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
if (intervals.size() <= 1) return intervals.size();
sort(intervals.begin(), intervals.end(),
[] (const auto& i, const auto& j) {
return i[0] < j[0];
});
priority_queue<int, vector<int>, greater<int>> minHeap;
int roomsRequired = 1;
minHeap.emplace(intervals[0][1]);
for (int i = 1; i < intervals.size(); i++) {
int earliestEndingTime = minHeap.top();
if (intervals[i][0] < earliestEndingTime) {
roomsRequired++;
} else {
minHeap.pop();
}
minHeap.emplace(intervals[i][1]);
}
return roomsRequired;
}
};
// Time: O(N^2), Space: O(1)
class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
if (intervals.size() <= 1) return intervals.size();
sort(intervals.begin(), intervals.end(),
[] (const auto& i, const auto& j) {
return i[0] < j[0];
});
int roomsRequired = 1;
for (int i = 1; i < intervals.size(); i++) {
int currRoomsRequired = 1;
for (int j = i - 1; j >= 0; j--) {
if (intervals[i][0] < intervals[j][1]) {
currRoomsRequired++;
}
}
roomsRequired = max(roomsRequired, currRoomsRequired);
}
return roomsRequired;
}
};