The Divide Intervals Into Minimum Number of Groups coding problem asks you to take a list of intervals [start, end] and partition them into the smallest possible number of groups such that no two intervals in the same group overlap. Two intervals overlap if they share any common point (e.g., [1, 5] and [5, 10] overlap at point 5).
Companies like Microsoft and Meta use this Greedy interview pattern to evaluate scheduling logic. It's essentially the same as finding the "maximum number of concurrent meetings" or the "minimum number of rooms required." It tests your ability to use Sorting and Heaps to manage overlapping time segments efficiently.
There are two main approaches:
end times of the current groups.start time is greater than the smallest end time in the heap, it can reuse that group (update the end time in the heap).end time into the heap).start as +1 and end + 1 as -1.Intervals: [[5, 10], [6, 8], [1, 5], [2, 3], [1, 10]]
[1, 5], [1, 10], [2, 3], [5, 10], [6, 8].[1, 5]: New group. Heap: [5].[1, 10]: Overlaps (1 <= 5). New group. Heap: [5, 10].[2, 3]: Overlaps. New group. Heap: [3, 5, 10].[5, 10]: Overlaps (5 <= 3 is false, but wait, rule says inclusive). If [2, 3] ends at 3, and [5, 10] starts at 5, they don't overlap. But point 5 is the issue. If the intervals were [1, 4] and [5, 10], they wouldn't overlap.
In this problem, the maximum number of overlapping intervals at any point in time equals the minimum number of groups needed.start == end constitutes an overlap.Memorize the "Meeting Rooms II" pattern. This problem is identical. The "Sweep Line" approach is often cleaner to code if you aren't comfortable with heap rebalancing.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Meeting Rooms II | Medium | Solve | |
| Zero Array Transformation III | Medium | Solve | |
| Maximum Total from Optimal Activation Order | Medium | Solve | |
| Maximum Subsequence Score | Medium | Solve | |
| Bag of Tokens | Medium | Solve |