Skip to main content

Divide Intervals Into Minimum Number of Groups - Solution & Explanation

MediumArrayTwo PointersGreedySorting16 min readAsked at: Amazon, Meta, IBM +4
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You are given several intervals [start, end]. Intervals that overlap cannot belong to the same group. The task is to divide all intervals into the minimum number of groups so that intervals inside each group never overlap.

The key observation: the answer equals the maximum number of overlapping intervals at any point in time. If three intervals overlap at the same moment, at least three groups are required.

Approach 1: Greedy Interval Grouping with Min Heap (O(n log n) time, O(n) space)

This approach uses sorting and a heap (priority queue). First sort intervals by their start time. Maintain a min‑heap that stores the end time of the last interval in each group. While iterating through the sorted intervals, check the smallest end time in the heap. If the current interval starts after that end time, reuse that group by popping from the heap. Otherwise, create a new group. Push the current interval's end into the heap. The heap size at any moment represents the number of active groups, and the maximum size reached is the answer. This greedy strategy works because reusing the group with the earliest finishing interval minimizes conflicts.

Approach 2: Sweep Line Algorithm (O(n log n) time, O(n) space)

The sweep line technique tracks how many intervals are active at each position. For every interval [l, r], add +1 at l and -1 at r + 1. Store these events and process them in sorted order. As you sweep from left to right, maintain a running sum of active intervals. The maximum value of this running sum is the number of overlapping intervals, which equals the minimum groups needed. This solution is closely related to the prefix sum technique and avoids managing individual groups explicitly.

Recommended for interviews: The greedy heap approach is usually expected because it demonstrates understanding of interval scheduling, greedy algorithms, and priority queues. The sweep line method is equally correct and often simpler conceptually when you recognize the problem as counting maximum overlaps. Showing both ideas signals strong mastery of interval problems.

Approach 1: Approach 1: Greedy Interval Grouping

This approach involves sorting intervals by starting times and then greedily finding the minimum number of groups. When an interval starts after the end of another interval, they can be in the same group; otherwise, they need different groups.

The key insight is to manage the end times of groups using a priority queue (or a min-heap).

The implementation sorts intervals based on the start time, uses the endTimes array to track end times of intervals in each group, and iteratively places each interval in the earliest non-overlapping group. If no such group is available, a new one is created.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n), due to sorting.

Space Complexity: O(n), for storing end times.

Try this approach in the editor →

Approach 2: Approach 2: Sweep Line Algorithm

This approach uses a sweep line algorithm where events are created for interval starts and ends. By tracking a count of ongoing intervals, the maximum number of overlapping intervals at any point can be determined, which corresponds to the minimum number of groups required.

It effectively converts the problem into finding the peak number of overlapping intervals.

The solution creates events for the start and the end (incremented by 1 for exclusive end point) of each interval. It sorts these events and uses a counter to track ongoing intervals, updating the maximum overlap found.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) for sorting events.

Space Complexity: O(n), events list.

Try this approach in the editor →

Approach 3: Greedy + Priority Queue (Min Heap)

First, we sort the intervals by their left endpoints. We use a min heap to maintain the rightmost endpoint of each group (the top of the heap is the minimum of the rightmost endpoints of all groups).

Next, we traverse each interval:

  • If the left endpoint of the current interval is greater than the top element of the heap, it means the current interval can be added to the group where the top element of the heap is located. We directly pop the top element of the heap, and then put the right endpoint of the current interval into the heap.
  • Otherwise, it means there is currently no group that can accommodate the current interval, so we create a new group and put the right endpoint of the current interval into the heap.

The time complexity is O(n times log n), and the space complexity is O(n). Here, n is the length of the array intervals.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Greedy Interval Grouping

Time Complexity: O(n log n), due to sorting.

Space Complexity: O(n), for storing end times.

Approach 2: Sweep Line Algorithm

Time Complexity: O(n log n) for sorting events.

Space Complexity: O(n), events list.

Greedy + Priority Queue (Min Heap)—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy Interval Grouping (Min Heap)O(n log n)O(n)General solution for interval grouping problems; ideal when managing active intervals dynamically
Sweep Line AlgorithmO(n log n)O(n)When the problem reduces to counting maximum overlaps rather than explicitly forming groups

Video Solution

Divide Intervals Into Minimum Number of Groups - Leetcode 2406 - Python • NeetCodeIO • 12,818 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Divide Intervals Into Minimum Number of Groups easy or hard?
The problem is rated Medium on LeetCode. It becomes straightforward once you recognize it as a maximum-overlapping-intervals problem similar to Meeting Rooms II.
Divide Intervals Into Minimum Number of Groups Python/Java solution
Python solutions typically use heapq after sorting intervals by start time. Java implementations use PriorityQueue for the same purpose. Both follow the same logic: reuse the group with the smallest end time or create a new group when overlaps occur.
How to solve Divide Intervals Into Minimum Number of Groups in O(n)?
An exact O(n) solution is generally not possible because intervals must be processed in sorted order of their endpoints. The closest optimal approach is O(n log n) using sorting combined with a heap or a sweep line event array.
What is the best approach for Divide Intervals Into Minimum Number of Groups?
The most common solution uses a greedy strategy with a min heap. Sort intervals by start time and track the earliest ending group in a priority queue. If the current interval starts after the earliest end, reuse that group; otherwise create a new one. This runs in O(n log n) time and is the standard interview approach.
Is Divide Intervals Into Minimum Number of Groups asked at Google/Amazon/Meta?
Interval overlap and meeting-room style problems frequently appear in interviews at companies like Google, Amazon, and Meta. This problem is a variation of the classic Meeting Rooms II pattern that tests sorting, greedy reasoning, and priority queue usage.
What data structure is used in Divide Intervals Into Minimum Number of Groups?
The greedy solution relies on a min heap (priority queue) to track the earliest finishing interval among active groups. The sweep line alternative uses an ordered map or event list combined with a prefix sum style accumulation.
What is the time complexity of Divide Intervals Into Minimum Number of Groups?
The optimal complexity is O(n log n). Sorting the intervals dominates the runtime, and heap operations during iteration also cost O(log n). Space complexity is O(n) for the heap or event list used to track active intervals.

Ready to solve this problem?

Practice Divide Intervals Into Minimum Number of Groups with our built-in code editor and test cases.

Practice on FleetCode