##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2406.html

# 2406. Divide Intervals Into Minimum Number of Groups

• Difficulty: Medium.
• Related Topics: .
• Similar Questions: Merge Intervals, Minimum Number of Frogs Croaking, Average Height of Buildings in Each Segment.

## Problem

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

## Solution (Java, C++, Python)

• class Solution {
public int minGroups(int[][] intervals) {
int m = 0;
for(int i[] : intervals){
m = Math.max(m,i);
m = Math.max(m,i);
}
long arr[] = new long[m+2];
for(int a[] : intervals){
arr[a] += 1;
arr[a+1] -= 1;
}
long max = 0l;
for(int i = 1; i <= m +1; i++){
arr[i] += arr[i-1];
max = Math.max(max,arr[i]);

}
return (int)max;
}
}

############

class Solution {
public int minGroups(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a - b);
PriorityQueue<Integer> q = new PriorityQueue<>();
for (var e : intervals) {
if (!q.isEmpty() && q.peek() < e) {
q.poll();
}
q.offer(e);
}
return q.size();
}
}

• class Solution:
def minGroups(self, intervals: List[List[int]]) -> int:
h = []
for a, b in sorted(intervals):
if h and h < a:
heappop(h)
heappush(h, b)
return len(h)

############

# 2406. Divide Intervals Into Minimum Number of Groups
# https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/

from sortedcontainers import SortedList

class Solution:
def minGroups(self, intervals: List[List[int]]) -> int:
n = len(intervals)
intervals.sort()
sl = SortedList(intervals)
res = 0

while len(sl) > 0:
s, e = sl
nextStart = e + 1
index = sl.bisect_left([nextStart, ])

while index < len(sl):
ns, ne = sl[index]
nextStart = ne + 1
sl.remove([ns, ne])
index = sl.bisect_left([nextStart, ])

sl.remove([s, e])
res += 1

return res


• class Solution {
public:
int minGroups(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
priority_queue<int, vector<int>, greater<int>> q;
for (auto& e : intervals) {
if (q.size() && q.top() < e) {
q.pop();
}
q.push(e);
}
return q.size();
}
};

• func minGroups(intervals [][]int) int {
sort.Slice(intervals, func(i, j int) bool { return intervals[i] < intervals[j] })
q := hp{}
for _, e := range intervals {
if q.Len() > 0 && q.IntSlice < e {
heap.Pop(&q)
}
heap.Push(&q, e)
}
return q.Len()
}

type hp struct{ sort.IntSlice }

func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{} {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}


Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).