Skip to content

Count Ways to Group Overlapping Ranges

MediumIntervals
Decode it first. Clues, constraints, pattern and plan before any code.

Split ranges into two groups (either group may be empty) so that any two ranges that overlap end up in the same group. Ranges are inclusive, so [1, 3] and [3, 5] overlap because they share 3. Return the number of ways to split them, modulo 10^9 + 7.

[[6, 10], [5, 15]]                  →  2
[[1, 3], [10, 20], [2, 5], [4, 8]]  →  4

In the second example, [1, 3], [2, 5] and [4, 8] are chained together and must share a group, and [10, 20] is on its own. Two independent blocks give 2 · 2 = 4 ways.

Constraints

  • 1 <= ranges.length <= 10^5
  • 0 <= start <= end <= 10^9