> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/array_problem/similar-to-253-2406.-divide-intervals-into-minimum-number-of-groups.md).

# (similar to 253 )2406. Divide Intervals Into Minimum Number of Groups

## actually it's same meaning to 253, but inclusive end time

## linesweep

end time is inclusive, so need to add 1

```java
class Solution {
    public int minGroups(int[][] intervals) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int[] interval : intervals) {
            map.put(interval[0], map.getOrDefault(interval[0], 0) + 1);
            map.put(interval[1]+1, map.getOrDefault(interval[1]+1, 0) - 1);
        }

        int roomCount = 0;
        int result = 0;
        for (int time : map.keySet()) {
            roomCount += map.get(time);
            result = Math.max(result, roomCount);
        }
        return result;
    }
}
```

## Heap (1942 style)

result + 1, because it's the group no, we need count, so +1

```java
class Solution {
    private record Group(int endTime, int group){};
    public int minGroups(int[][] intervals) {
        int n = intervals.length;
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        Queue<Integer> av = new PriorityQueue<>();
        Queue<Group> oc = new PriorityQueue<>((a, b) -> a.endTime - b.endTime);
        for (int i = 0; i < n; i++) {
            av.offer(i);
        }
        int result = 0;
        for (int[] in : intervals) {
            int start = in[0];
            int end = in[1];
            while (!oc.isEmpty() && oc.peek().endTime < start) {
                av.offer(oc.poll().group);
            }
            int group = av.poll();
            oc.offer(new Group(end, group));
            result = Math.max(result, group);
        }
        return result + 1;
    }
}
```

## Heap

one heap

```java
class Solution {
    public int minGroups(int[][] intervals) {
        int n = intervals.length;
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        Queue<Integer> endTimeHeap = new PriorityQueue<>();

        int result = 0;
        for (int[] in : intervals) {
            int start = in[0];
            int end = in[1];
            while (!endTimeHeap.isEmpty() && endTimeHeap.peek() < start) {
                endTimeHeap.poll();
            }
            endTimeHeap.offer(end);
            result = Math.max(result, endTimeHeap.size());
        }
        return result;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/array_problem/similar-to-253-2406.-divide-intervals-into-minimum-number-of-groups.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
