A set of real numbers can be represented as the union of several disjoint intervals, where each interval is in the form [a, b). A real number x is in the set if one of its intervals [a, b) contains x (i.e. a <= x < b).
You are given a sorted list of disjoint intervals intervals representing a set of real numbers as described above, where intervals[i] = [ai, bi]
represents the interval [ai, bi). You are also given another interval
toBeRemoved.
Return the set of real numbers with the intervaltoBeRemovedremoved from __intervals. In other words, return the set of real numbers such that everyxin the set is inintervals _butnot in _toBeRemoved.
Your answer should be asorted list of disjoint intervals as described above.
For each interval, if it overlaps with the interval to be removed, we split it into at most two intervals: the part before the removal and the part after. If it doesn’t overlap, we keep it as is.
funcremoveInterval(intervals [][]int, toBeRemoved []int) [][]int {
res:= [][]int{}
remL, remR:=toBeRemoved[0], toBeRemoved[1]
for_, it:=rangeintervals {
l, r:=it[0], it[1]
ifr<=remL||l>=remR {
res = append(res, []int{l, r})
} else {
ifl < remL {
res = append(res, []int{l, min(r, remL)})
}
ifr > remR {
res = append(res, []int{max(l, remR), r})
}
}
}
returnres}
func min(a, bint) int { ifa < b { returna }; returnb }
func max(a, bint) int { ifa > b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
public List<List<Integer>>removeInterval(int[][] intervals, int[] toBeRemoved) {
List<List<Integer>> res =new ArrayList<>();
int remL = toBeRemoved[0], remR = toBeRemoved[1];
for (int[] it : intervals) {
int l = it[0], r = it[1];
if (r <= remL || l >= remR) {
res.add(Arrays.asList(l, r));
} else {
if (l < remL) res.add(Arrays.asList(l, Math.min(r, remL)));
if (r > remR) res.add(Arrays.asList(Math.max(l, remR), r));
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funremoveInterval(intervals: Array<IntArray>, toBeRemoved: IntArray): List<List<Int>> {
val res = mutableListOf<List<Int>>()
val remL = toBeRemoved[0]
val remR = toBeRemoved[1]
for (itin intervals) {
val l = it[0]
val r = it[1]
if (r <= remL || l >= remR) {
res.add(listOf(l, r))
} else {
if (l < remL) res.add(listOf(l, minOf(r, remL)))
if (r > remR) res.add(listOf(maxOf(l, remR), r))
}
}
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from typing import List
classSolution:
defremoveInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
res = []
remL, remR = toBeRemoved
for l, r in intervals:
if r <= remL or l >= remR:
res.append([l, r])
else:
if l < remL:
res.append([l, min(r, remL)])
if r > remR:
res.append([max(l, remR), r])
return res
impl Solution {
pubfnremove_interval(intervals: Vec<Vec<i32>>, to_be_removed: Vec<i32>) -> Vec<Vec<i32>> {
letmut res = Vec::new();
let (rem_l, rem_r) = (to_be_removed[0], to_be_removed[1]);
for it in intervals.iter() {
let (l, r) = (it[0], it[1]);
if r <= rem_l || l >= rem_r {
res.push(vec![l, r]);
} else {
if l < rem_l {
res.push(vec![l, r.min(rem_l)]);
}
if r > rem_r {
res.push(vec![l.max(rem_r), r]);
}
}
}
res
}
}