Trains the technique from
LeetCode 2580Count Ways to Group Overlapping RangesThis 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.
Example 1
The two spans share no number, so each may go into either set freely, which is four ways.
Example 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
A single span may go into either set.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_ways(spans: list[list[int]]) -> int:public int countWays(int[][] spans)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.