You are given a 2D integer array ranges where ranges[i] = [starti, endi]
denotes that all integers between starti and endi (both inclusive) are contained in the ith range.
You are to split ranges into two (possibly empty) groups such that:
Each range belongs to exactly one group.
Any two overlapping ranges must belong to the same group.
Two ranges are said to be overlapping if there exists at least one integer that is present in both ranges.
For example, [1, 3] and [2, 5] are overlapping because 2 and 3 occur in both ranges.
Return thetotal number of ways to splitrangesinto two groups.
Since the answer may be very large, return it modulo10^9 + 7.
Input: ranges =[[6,10],[5,15]]Output: 2Explanation:
The two ranges are overlapping, so they must be in the same group.Thus, there are two possible ways:- Put both the ranges together in group 1.- Put both the ranges together in group 2.
Input: ranges =[[1,3],[10,20],[2,5],[4,8]]Output: 4Explanation:
Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group.Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group.Thus, there are four possible ways to group them:- All the ranges in group 1.- All the ranges in group 2.- Ranges [1,3],[2,5], and [4,8]in group 1 and [10,20]in group 2.- Ranges [1,3],[2,5], and [4,8]in group 2 and [10,20]in group 1.
The key idea is to treat each group of overlapping ranges as a connected component. Any two overlapping ranges must be in the same group, so the number of ways to split the ranges is 2^c, where c is the number of connected components (groups of overlapping intervals). Each group can be assigned to either of the two groups independently.
classSolution {
public:int countWays(vector<vector<int>>& ranges) {
constint MOD =1e9+7;
sort(ranges.begin(), ranges.end());
int c =0, end =-1;
for (auto& r : ranges) {
if (r[0] > end) {
++c;
end = r[1];
} else {
end = max(end, r[1]);
}
}
longlong ans =1;
while (c--) ans = ans *2% MOD;
return ans;
}
};
funccountWays(ranges [][]int) int {
constmod = 1e9+7sort.Slice(ranges, func(i, jint) bool { returnranges[i][0] < ranges[j][0] })
c, end:=0, -1for_, r:=rangeranges {
ifr[0] > end {
c++end = r[1]
} else {
ifr[1] > end {
end = r[1]
}
}
}
ans:=1fori:=0; i < c; i++ {
ans = ans*2%mod }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintcountWays(int[][] ranges) {
int MOD = 1_000_000_7;
Arrays.sort(ranges, (a, b) -> Integer.compare(a[0], b[0]));
int c = 0, end =-1;
for (int[] r : ranges) {
if (r[0]> end) {
c++;
end = r[1];
} else {
end = Math.max(end, r[1]);
}
}
long ans = 1;
for (int i = 0; i < c; i++) ans = ans * 2 % MOD;
return (int) ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funcountWays(ranges: Array<IntArray>): Int {
val MOD = 1_000_000_7
ranges.sortBy { it[0] }
var c = 0var end = -1for (r in ranges) {
if (r[0] > end) {
c++ end = r[1]
} else {
end = maxOf(end, r[1])
}
}
var ans = 1L repeat(c) { ans = ans * 2 % MOD }
return ans.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defcountWays(self, ranges: list[list[int]]) -> int:
MOD =10**9+7 ranges.sort()
c =0 end =-1for s, e in ranges:
if s > end:
c +=1 end = e
else:
end = max(end, e)
ans =1for _ in range(c):
ans = ans *2% MOD
return ans
impl Solution {
pubfncount_ways(ranges: Vec<Vec<i32>>) -> i32 {
letmut ranges = ranges;
ranges.sort();
letmut c =0;
letmut end =-1;
for r in ranges.iter() {
if r[0] > end {
c +=1;
end = r[1];
} else {
end = end.max(r[1]);
}
}
letmut ans =1i64;
for _ in0..c {
ans = ans *2%1_000_000_007;
}
ans asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
countWays(ranges: number[][]):number {
constMOD=1e9+7;
ranges.sort((a, b) =>a[0] -b[0]);
letc=0, end=-1;
for (const [s, e] ofranges) {
if (s>end) {
c++;
end=e;
} else {
end= Math.max(end, e);
}
}
letans=1;
for (leti=0; i<c; i++) ans=ans*2%MOD;
returnans;
}
}