-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeOverlappingIntervals.java
54 lines (45 loc) 路 1.31 KB
/
MergeOverlappingIntervals.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
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.*;
class Problem {
public int[][] merge(int[][] intervals) {
Pair[] pairs = new Pair[intervals.length];
for (int i = 0; i < intervals.length; i++) {
pairs[i] = new Pair(intervals[i][0],intervals[i][1]);
}
Stack<Pair> st = new Stack<>();
Arrays.sort(pairs);
for (int i = 0; i < intervals.length; i++) {
if (st.empty()) st.push(pairs[i]);
else{
if (st.peek().x <= pairs[i].y){
st.peek().x = st.peek().x < pairs[i].x?st.peek().x:pairs[i].x;
}else st.push(pairs[i]);
}
}
int[][] ans = new int[st.size()][2];
int i=0;
while (!st.empty()){
ans[i][0] = st.peek().x;
ans[i][1] = st.peek().y;
st.pop();
i++;
}
return ans;
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair other){
if (this.y != other.y){
return other.y - this.y;
}else {
return this.y - other.y;
}
}
}
}