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
        5---------10
          6---8
1-------5
  2-3
1-----------------10
Inputintervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]
Output3
We can divide the intervals into three groups, and it can be proven that fewer than 3 groups is not possible.
Example 2
1---3
        5-6
              8---10
                    11--13
Inputintervals = [[1,3],[5,6],[8,10],[11,13]]
Output1
None of the intervals overlap, so we can put all of them in one group.

Constraints

  • 1 <= intervals.length <= 10^5
  • intervals[i].length == 2
  • 1 <= lefti <= righti <= 10^6

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate