-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.cpp
35 lines (29 loc) · 941 Bytes
/
solution.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
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Solution
{
public:
int minGroups(vector<vector<int>> &intervals)
{
// Sort intervals by their start time
sort(intervals.begin(), intervals.end());
// Min-heap to track the end times of intervals in groups
priority_queue<int, vector<int>, greater<int>> pq;
// Traverse through all intervals
for (const auto &interval : intervals)
{
int start = interval[0], end = interval[1];
// If the earliest end time is less than the current start, reuse that group
if (!pq.empty() && pq.top() < start)
{
pq.pop();
}
// Push the end time of the current interval into the heap
pq.push(end);
}
// The size of the heap at the end is the number of groups
return pq.size();
}
};