-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_Intervals.java
61 lines (52 loc) · 1.75 KB
/
Merge_Intervals.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.leet_code;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Merge_Intervals {
public static void main(String[] args) {
int[][]a={{1,3},{2,6},{8,10},{15,18}};
merge2(a);
// for (int i = 0; i < x.length; i++) {
// System.out.print(Arrays.toString(x[i]));
// }
}
public static int[][] merge(int[][] intervals) {//only 63 test case
List<int[]> x=new ArrayList<>();
int s=intervals[0][0];
int e=intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
if ( e>=intervals[i][0]) {
e=intervals[i][1];
}else {
int[]z={s,e};
x.add(z);
s=intervals[i][0];
e=intervals[i][1];
}
}
int[]z={s,e};
x.add(z);
int[][] array = new int[x.size()][];
for(int i = 0; i < x.size(); i++) array[i] = x.get(i);
return array;
}
public static int[][] merge2(int[][] intervals) { //efficent all test cases
if (intervals.length <= 1)
return intervals;
//Arrays.sort(intervals, (arr1, arr2) -> Integer.compare(arr1[0], arr2[0]));
Arrays.sort(intervals, Comparator.comparingInt(i -> i[0]));
List<int[]> result = new ArrayList<>();
int[] newInterval = intervals[0];
result.add(newInterval);
for (int[] x : intervals) {
if (newInterval[1] >= x[0])
newInterval[1] = Math.max(newInterval[1], x[1]);
else {
newInterval = x;
result.add(newInterval);
}
}
return result.toArray(new int[result.size()][]);
}
}