All problems
1091MediumArraySorting

Splitting Overlapping Spans Into Two Sets

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2580Count Ways to Group Overlapping Ranges

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Spans are given as spans, where spans[i] = [from, to] covers every whole number from from up to to, both ends included. Two spans overlap when some whole number lies in both.

Put every span into one of two sets, called the first and the second, so that no number lies in a span of the first set and also in a span of the second. Either set may be left empty.

Return how many ways there are to do it, given as the remainder after dividing by 1000000007.

Examples

Example 1

Input
spans = [[1, 2], [3, 4]]
Output
4

The two spans share no number, so each may go into either set freely, which is four ways.

Example 2

Input
spans = [[1, 5], [2, 3]]
Output
2

The second span lies inside the first, so both must go into the same set, and that set may be either of the two.

Example 3

Input
spans = [[0, 0]]
Output
2

A single span may go into either set.

Constraints

  • 1 <= spans.length <= 10^5
  • spans[i].length == 2
  • 0 <= spans[i][0] <= spans[i][1] <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_ways(spans: list[list[int]]) -> int:
Java
public int countWays(int[][] spans)
September 7
Apply