Insert Interval
Add a new interval to a sorted list of intervals, merging it with every interval it overlaps, in one pass.
Each interval is a pair [start, end]intervalsnewInterval[4, 8][8, 10]
Example 1
Output: [[1,5],[6,9]]
Example 2
Output: [[1,2],[3,10],[12,16]]
Explanation: [4,8] overlaps [3,5], [6,7] and [8,10].
The Idea
Picture a day planner with some hours already blocked out: 1 to 2, 3 to 5, 6 to 7, 8 to 10 and 12 to 16. Each block is an interval, a stretch from a start to an end, written [3, 5]
Look at the picture below. It shows Example 2 on a number line, with the new interval [4, 8]
In the row labelled "existing intervals", [1, 2][3, 5][6, 7][8, 10][12, 16]
Notice the order of the labels: all "before" intervals come first, then all "overlapping" ones, then all "after" ones. That is no accident. The list is sorted by start and has no overlaps, so its intervals sit one after another along the line, and the new interval covers one connected stretch of it. So how do we tell which group each interval belongs to? By walking the list once, from left to right, and noticing where one group gives way to the next.
The overlapping group and the new interval then join into one interval. It starts at the smallest start among them, 3, and ends at the largest end, 10. That is [3, 10][1, 2][12, 16]
Solution Approach
Sorting again after adding works but does extra work, so one pass is used instead, settling three things: where the "before" group ends, how the new interval grows, and where the grown interval goes.
Adding and sorting again
Why not append newInterval
Where the "before" group ends
The walk moves an index iintervals[i][1] < startstartstart
Growing the new interval
Once an interval no longer ends before startendintervals[i][0] <= endstartstart
Each such interval widens the new one: startend[3, 5][4, 8][3, 8][6, 7][8, 10][3, 10][12, 16]
Where the grown interval goes
Where does [start, end]end
Java Solution
The method insert
- First, create an empty list
, set an indexresult to 0 andi to the number of intervals, and copy the new interval's ends inton andstart .end - A first loop copies the "before" group: while
, it addsintervals[i][1] < start tointervals[i] and movesresult on.i - A second loop, starting where the first stopped, grows the new interval: while
, it setsintervals[i][0] <= end to the smaller start andstart to the larger end, and movesend on.i - Add
to[start, end] .result - A third loop copies the rest of
intointervals , and the method returnsresult as an array withresult .result.toArray(new int[0][])
The main
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class InsertInterval {
public static int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> result = new ArrayList<>();
int i = 0;
int n = intervals.length;
int start = newInterval[0];
int end = newInterval[1];
while (i < n && intervals[i][1] < start) {
result.add(intervals[i++]); // ends before the new one starts
}
while (i < n && intervals[i][0] <= end) {
start = Math.min(start, intervals[i][0]); // overlaps: widen to cover it
end = Math.max(end, intervals[i][1]);
i++;
}
result.add(new int[] {start, end});
while (i < n) {
result.add(intervals[i++]); // starts after the new one ends
}
return result.toArray(new int[0][]);
}
public static void main(String[] args) {
System.out.println(Arrays.deepToString(
insert(new int[][] {{1, 3}, {6, 9}}, new int[] {2, 5})));
System.out.println(Arrays.deepToString(
insert(new int[][] {{1, 2}, {3, 5}, {6, 7}, {8, 10}, {12, 16}}, new int[] {4, 8})));
System.out.println(Arrays.deepToString(
insert(new int[][] {}, new int[] {5, 7})));
System.out.println(Arrays.deepToString(
insert(new int[][] {{1, 2}}, new int[] {2, 3})));
System.out.println(Arrays.deepToString(
insert(new int[][] {{3, 4}, {6, 7}}, new int[] {1, 2})));
System.out.println(Arrays.deepToString(
insert(new int[][] {{3, 4}, {6, 7}}, new int[] {9, 10})));
System.out.println(Arrays.deepToString(
insert(new int[][] {{3, 4}, {6, 7}}, new int[] {1, 10})));
}
}
Running it prints:
[[1, 5], [6, 9]]
[[1, 2], [3, 10], [12, 16]]
[[5, 7]]
[[1, 3]]
[[1, 2], [3, 4], [6, 7]]
[[3, 4], [6, 7], [9, 10]]
[[1, 10]]
[[1, 5], [6, 9]][[1, 2], [3, 10], [12, 16]][4, 8][3, 10][[5, 7]]
Look at the fourth line, [[1, 3]][1, 2][2, 3]<<=[1, 2][[1, 2], [2, 3]]
The last three lines all insert into [[3, 4], [6, 7]][1, 2][9, 10][1, 10]
Complexity
The three loops share one index i
- Time: O(n), because
passes each of the n existing intervals once across the three loops, andi copies at most n + 1 entries.toArray - Space: O(n) for the result, because
holds up to n + 1 intervals, while the walk itself keeps onlyresult ,start andend .i