To solve this problem, we can use a sweep line algorithm. The main idea is to process all the interval boundaries (both start and end points) in a sorted order while keeping track of the number of overlapping intervals at any point in time.
Here are the detailed steps:
-
Event Representation:
- For each interval
[left, right], we create two events:
- A start event at
left (denoted by type 1).
- An end event at
right (denoted by type -1).
-
Sorting Events:
- We sort all the events based on their positions.
- If two events occur at the same position, we process the end event before the start event. This is crucial because if an interval ends at the same point where another interval starts, they are considered overlapping as per the problem statement.
-
Processing Events:
- We traverse the sorted events, updating a counter that keeps track of the number of active (overlapping) intervals.
- The maximum value of this counter during the traversal will give us the minimum number of groups required.
This approach has a time complexity of O(N log N) due to the sorting step, which is acceptable given the problem constraints.
Here is the C++ implementation based on the above approach:
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int minGroups(vector<vector<int>>& intervals) {
// Create a list of events
// Each event is a pair: (position, type)
// type = 1 for start, -1 for end
vector<pair<int, int>> events;
events.reserve(2 * intervals.size());
for(auto &interval : intervals){
events.emplace_back(interval[0], 1); // Start of interval
events.emplace_back(interval[1], -1); // End of interval
}
// Sort the events
// If two events have the same position, process end event first
sort(events.begin(), events.end(), [&](const pair<int,int> &a, const pair<int,int> &b) -> bool {
if(a.first != b.first)
return a.first < b.first;
return a.second < b.second; // end (-1) before start (1)
});
int current = 0;
int max_groups = 0;
for(auto &event : events){
current += event.second;
max_groups = max(max_groups, current);
}
return max_groups;
}
};
Explanation of the Code:
-
Event Creation:
- We iterate through each interval and add two events to the
events vector: one for the start and one for the end.
-
Sorting Events:
- We sort the
events vector with a custom comparator:
- Primary Sort Key: The position of the event.
- Secondary Sort Key: The type of event, where end events (
-1) come before start events (1) if positions are equal. This ensures that intervals ending at a point are considered before new intervals starting at the same point, maintaining the overlap condition.
-
Processing the Sorted Events:
- We initialize two variables:
current to keep track of the number of active intervals at any point, and max_groups to store the maximum number of overlapping intervals encountered.
- As we iterate through the sorted events:
- For a start event, we increment
current.
- For an end event, we decrement
current.
- After each update, we check if the current number of active intervals is greater than
max_groups and update max_groups accordingly.
-
Final Result:
- The value of
max_groups after processing all events will be the minimum number of groups required to ensure no overlapping intervals within a group.
Example Walkthrough:
Let's take Example 1 from the problem statement:
Input: intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]
-
Events:
(1, 1), (5, -1)
(2, 1), (3, -1)
(5, 1), (10, -1)
(6, 1), (8, -1)
(1, 1), (10, -1)
-
Sorted Events:
(1, -1), (1, 1), (1, 1), (2, 1), (3, -1), (5, -1), (5, 1), (6, 1), (8, -1), (10, -1), (10, -1)
-
Processing:
- At position
1, two start events: current = 2, max_groups = 2.
- At position
2, one start event: current = 3, max_groups = 3.
- At position
3, one end event: current = 2.
- At position
5, one end and one start event: current = 2.
- At position
6, one start event: current = 3.
- At position
8, one end event: current = 2.
- At position
10, two end events: current = 0.
The maximum number of overlapping intervals is 3, so the minimum number of groups required is 3.
This approach ensures that we efficiently determine the minimum number of groups by effectively tracking overlapping intervals.