-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge Intervals
More file actions
52 lines (44 loc) · 1.62 KB
/
Merge Intervals
File metadata and controls
52 lines (44 loc) · 1.62 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
49
50
51
52
class Solution {
static class pair implements Comparable<pair> {
int start, end;
pair(int a, int b){
this.start = a;
this.end= b;
}
public int compareTo(pair other){
return this.start- other.start;
}
}
public int[][] merge(int[][] intervals) {
int n = intervals.length;
if (n <= 1) return intervals;
pair[] records = new pair[n];
for (int i = 0; i < n; i++) {
records[i] = new pair(intervals[i][0] , intervals[i][1]);
}
Arrays.sort(records);
// for (int i = 0; i < n; i++)
// System.out.println(records[i].start + " " + records[i].end);
int mainStart = records[0].start, mainEnd = records[0].end;
ArrayList<pair> arr = new ArrayList<>();
for (int i = 1; i < n; i++) {
if (records[i].start <= mainEnd) {
mainEnd = Math.max(mainEnd, records[i].end);
// System.out.println("here");
} else {
arr.add(new pair(mainStart, mainEnd));
mainStart = records[i].start;
mainEnd = records[i].end;
// System.out.println("here 2");
}
}
arr.add(new pair(mainStart, mainEnd));
int[][] ans = new int[arr.size()][2];
for (int i =0; i <arr.size(); i++) {
// System.out.println(arr.get(i).start + " " + arr.get(i).end);
ans[i][0] = arr.get(i).start;
ans[i][1] = arr.get(i).end;
}
return ans;
}
}