|
| 1 | +class Solution { |
| 2 | + public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) { |
| 3 | + List<List<Integer>> list = new ArrayList<>(); |
| 4 | + int removeStart = toBeRemoved[0]; |
| 5 | + int removeEnd = toBeRemoved[1]; |
| 6 | + for (int[] interval : intervals) { |
| 7 | + int start = interval[0]; |
| 8 | + int end = interval[1]; |
| 9 | + // Case 1: Overlap the removal interval. Skip this interval |
| 10 | + if (start >= removeStart && end <= removeEnd) { |
| 11 | + continue; |
| 12 | + } |
| 13 | + // Case 2 and 3: Completely miss out the removal interval either on left side or right side of number line. |
| 14 | + // Add the complete interval |
| 15 | + if ((start <= removeStart && end <= removeStart) || |
| 16 | + (start >= removeEnd && end >= removeEnd)) { |
| 17 | + list.add(Arrays.asList(start, end)); |
| 18 | + continue; |
| 19 | + } |
| 20 | + // Case 4: Interval partially overlaps on left side of removal interval on number line. |
| 21 | + // Add the modified interval |
| 22 | + if (start <= removeStart) { |
| 23 | + int tempStart = Math.min(start, removeStart); |
| 24 | + int tempEnd = Math.max(start, removeStart); |
| 25 | + if (tempStart != tempEnd) { |
| 26 | + list.add(Arrays.asList(tempStart, tempEnd)); |
| 27 | + } |
| 28 | + } |
| 29 | + // Case 5: Interval partially overlaps on right side of removal interval on number line. |
| 30 | + // Add the modified interval |
| 31 | + if (end >= removeEnd) { |
| 32 | + int tempStart = Math.min(end, removeEnd); |
| 33 | + int tempEnd = Math.max(end, removeEnd); |
| 34 | + if (tempStart != tempEnd) { |
| 35 | + list.add(Arrays.asList(tempStart, tempEnd)); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return list; |
| 40 | + } |
| 41 | +} |
0 commit comments