2406. Divide Intervals Into Minimum Number of Groups LeetCode Solution
In this guide, you will get 2406. Divide Intervals Into Minimum Number of Groups LeetCode Solution with the best time and space complexity. The solution to Divide Intervals Into Minimum Number of Groups problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Divide Intervals Into Minimum Number of Groups solution in C++
- Divide Intervals Into Minimum Number of Groups solution in Java
- Divide Intervals Into Minimum Number of Groups solution in Python
- Additional Resources

Problem Statement of Divide Intervals Into Minimum Number of Groups
You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents the inclusive interval [lefti, righti].
You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other.
Return the minimum number of groups you need to make.
Two intervals intersect if there is at least one common number between them. For example, the intervals [1, 5] and [5, 8] intersect.
Example 1:
Input: intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]
Output: 3
Explanation: We can divide the intervals into the following groups:
– Group 1: [1, 5], [6, 8].
– Group 2: [2, 3], [5, 10].
– Group 3: [1, 10].
It can be proven that it is not possible to divide the intervals into fewer than 3 groups.
Example 2:
Input: intervals = [[1,3],[5,6],[8,10],[11,13]]
Output: 1
Explanation: None of the intervals overlap, so we can put all of them in one group.
Constraints:
1 <= intervals.length <= 105
intervals[i].length == 2
1 <= lefti <= righti <= 106
Complexity Analysis
- Time Complexity: O(n\log n)
- Space Complexity: O(n)
2406. Divide Intervals Into Minimum Number of Groups LeetCode Solution in C++
class Solution {
public:
// Similar to 253. Meeting Rooms II
int minGroups(vector<vector<int>>& intervals) {
// Stores `right`s.
priority_queue<int, vector<int>, greater<>> minHeap;
ranges::sort(intervals);
for (const vector<int>& interval : intervals) {
// There's no overlap, so we can reuse the same group.
if (!minHeap.empty() && interval[0] > minHeap.top())
minHeap.pop();
minHeap.push(interval[1]);
}
return minHeap.size();
}
};
/* code provided by PROGIEZ */
2406. Divide Intervals Into Minimum Number of Groups LeetCode Solution in Java
class Solution {
// Similar to 253. Meeting Rooms II
public int minGroups(int[][] intervals) {
// Stores `right`s.
Queue<Integer> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a, b));
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
for (int[] interval : intervals) {
// There's no overlap, so we can reuse the same group.
if (!minHeap.isEmpty() && interval[0] > minHeap.peek())
minHeap.poll();
minHeap.offer(interval[1]);
}
return minHeap.size();
}
}
// code provided by PROGIEZ
2406. Divide Intervals Into Minimum Number of Groups LeetCode Solution in Python
class Solution:
# Similar to 253. Meeting Rooms II
def minGroups(self, intervals: list[list[int]]) -> int:
minHeap = [] # Stores `right`s.
for left, right in sorted(intervals):
# There's no overlap, so we can reuse the same group.
if minHeap and left > minHeap[0]:
heapq.heappop(minHeap)
heapq.heappush(minHeap, right)
return len(minHeap)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.