-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
37 lines (34 loc) · 1007 Bytes
/
Solution.java
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
28
29
30
31
32
33
34
35
36
37
package $057;
import datastruc.Interval;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 9:49 AM 2018/07/19.
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
intervals.add(newInterval);
int n = intervals.size();
int[] starts = new int[n], ends = new int[n];
int i = 0;
for (Interval interval : intervals){
starts[i] = interval.start;
ends[i] = interval.end;
i++;
}
Arrays.sort(starts);
Arrays.sort(ends);
List<Interval> list = new ArrayList<>();
// j is start of interval.
int j;
for (i = 0, j = 0; i < n; i++) {
if (i == n - 1 || starts[i + 1] > ends[i]) {
list.add(new Interval(starts[j], ends[i]));
j = i + 1;
}
}
return list;
}
}