-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathP16.java
More file actions
48 lines (38 loc) · 1.19 KB
/
P16.java
File metadata and controls
48 lines (38 loc) · 1.19 KB
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
38
39
40
41
42
43
44
45
46
47
48
package stack_and_queue;
import java.util.*;
// Merge Overlapping Intervals
public class P16 {
public static int[][] merge(int[][] intervals) {
//Arrays.sort(intervals, (a,b) );
ArrayList<int[]> res = new ArrayList<>();
if(res.size() == 0 || intervals == null) {
res.toArray(new int[0][]);
}
Arrays.sort(intervals, (a,b) -> a[0] - b[0]);
int start = intervals[0][0];
int end = intervals[0][1];
for(int[] a: intervals) {
int[] prev = res.get(res.size() - 1);
if(a[0] <= prev[1])
res.get(res.size() - 1)[1] = Math.max(a[1], prev[1]);
else {
res.add(a);
start = prev[0];
end = prev[1];
}
}
res.add(new int[]{start, end});
return res.toArray(new int[0][]);
// int n = res.size();
// int[][] arr = new int[n][2];
// int idx = 0;
// for(int[] a: res) {
// arr[idx][0] = a[0];
// arr[idx][1] = a[1];
// idx++;
// }
// return arr;
}
public static void main(String[] args) {
}
}