-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path42_meeting-rooms-ii.cpp
81 lines (69 loc) · 2.09 KB
/
42_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
72
73
74
75
76
77
78
79
80
81
// DATE: 06-Aug-2023
/* PROGRAM: 42_Interval - Meeting Rooms II
https://www.lintcode.com/problem/919/
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.)
Note: (0,8),(8,10) is not conflict at 8
Example 1:
Input: intervals = [(0,30),(5,10),(15,20)]
Output: 2
Explanation: We need two meeting rooms
room1: (0,30)
room2: (5,10),(15,20)
Example 2:
Input: intervals = [(2,7)]
Output: 1
Explanation: Only need one meeting room
*/
// @ankitsamaddar @Aug_2023
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// 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) {
sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b) {
if (a.start != b.start) {
return a.start < b.start;
} else
return a.start < b.start;
});
// total rooms
priority_queue<int, vector<int>, greater<int>> pq;
// 1st meeting
pq.push(intervals[0].end);
for (int i = 1; i < intervals.size(); i++) {
int start = intervals[i].start;
if (start >= pq.top()) {
pq.pop(); // previous meeting ends before current so room not required
}
// reallocate old room
// add new room
pq.push(intervals[i].end);
}
// size of the queue is the total rooms required
return pq.size();
}
};
int main() {
vector<Interval> nums = {{65, 424}, {351, 507}, {314, 807}, {387, 722},
{19, 797}, {259, 722}, {165, 221}, {136, 897}};
Solution sol;
cout << sol.minMeetingRooms(nums); // Output: 7
return 0;
}