Skip to content
Decoder

Count Ways to Group Overlapping Ranges

Medium

Problem

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.

Example 1

Input
[[6, 10], [5, 15]]
Output
2

Example 2

Input
[[1, 3], [10, 20], [2, 5], [4, 8]]
Output
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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.