-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_435.java
More file actions
42 lines (39 loc) · 1.12 KB
/
a_435.java
File metadata and controls
42 lines (39 loc) · 1.12 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
package greedy;
import java.util.Arrays;
import java.util.Comparator;
/**
* 无重叠区间
*/
public class a_435 {
public int eraseOverlapIntervals(Interval[] intervals) {
//将intervals数组中元素按end排序
Arrays.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
//if (o1.end > o2.end) return 1;
//else if (o1.end < o2.end) return -1;
//else return 0;
return o1.end - o2.end;
}
});
//nowEnd记录Interval类的end
int nowEnd = intervals[0].end;
//num记录需要删除的元素
int num = 0;
//遍历intervals
for (int i = 1; i < intervals.length; i++) {
if (nowEnd <= intervals[i].start) {
nowEnd = intervals[i].end;
continue;
}
num++;
}
return num;
}
class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
}
}