-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path435.non-overlapping-intervals.java
More file actions
51 lines (43 loc) · 1.45 KB
/
Copy path435.non-overlapping-intervals.java
File metadata and controls
51 lines (43 loc) · 1.45 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
class Solution {
HashMap<Integer, ArrayList<Integer>> hsm;
HashSet<Integer> vis;
int res;
public int eraseOverlapIntervals(int[][] intervals) {
hsm = new HashMap<Integer, ArrayList<Integer>>();
vis = new HashSet<Integer>();
res = 0;
// System.out.println(intervals.length);
if(intervals.length == 0 || intervals.length == 1 && intervals[0].length == 0)
return res;
Arrays.sort(intervals, new Comparator<int[]>(){
@Override
public int compare(int[] a, int[] b){
// if(a[1] == b[1]){
// if(a[0] < b[0])
// return -1;
// else
// return 1;
// }
// else
// if(a[0]+a[1] == b[0]+b[1])
// {
// if(a[])
// }
if(a[0]+a[1] < b[0]+b[1])
return -1;
return 1;
}
});
int prev = intervals[0][1];
for(int[] interval: intervals){
// System.out.println(interval[0]+" "+interval[1]);
if(prev > interval[0]){
res++;
if(prev <= interval[1])
continue;
}
prev = interval[1];
}
return res> 0 ? res-1: res;
}
}