-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_57.java
More file actions
27 lines (24 loc) · 845 Bytes
/
Copy pathP_57.java
File metadata and controls
27 lines (24 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package leetcode.hard;
import java.util.ArrayList;
import java.util.List;
public class P_57 {
public int[][] insert(int[][] intervals, int[] newInterval) {
final List<int[]> res = new ArrayList<>();
for (int[] interval : intervals) {
if (newInterval == null || interval[1] < newInterval[0]) {
res.add(interval);
} else if (interval[0] > newInterval[1]) {
res.add(newInterval);
res.add(interval);
newInterval = null;
} else {
newInterval[0] = Math.min(newInterval[0], interval[0]);
newInterval[1] = Math.max(newInterval[1], interval[1]);
}
}
if (newInterval != null) {
res.add(newInterval);
}
return res.toArray(int[][]::new);
}
}