mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0435.无重叠区间.md
First java code not working, I test in the IntellJ with jdk 11, the Arrays.sort is not right ( doesn't make the left edge sort from small to big ), so the non overlapping area count fail. I fix it. Sort by end first, if end is same, sort by start
This commit is contained in:
@ -186,27 +186,27 @@ Java:
|
||||
class Solution {
|
||||
public int eraseOverlapIntervals(int[][] intervals) {
|
||||
if (intervals.length < 2) return 0;
|
||||
|
||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||
@Override
|
||||
public int compare(int[] o1, int[] o2) {
|
||||
if (o1[0] != o2[0]) {
|
||||
if (o1[1] != o2[1]) {
|
||||
return Integer.compare(o1[1],o2[1]);
|
||||
} else {
|
||||
return Integer.compare(o2[0],o1[0]);
|
||||
return Integer.compare(o1[0],o2[0]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
int count = 0;
|
||||
int count = 1;
|
||||
int edge = intervals[0][1];
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
if (intervals[i][0] < edge) {
|
||||
count++;
|
||||
} else {
|
||||
if (edge <= intervals[i][0]){
|
||||
count ++; //non overlap + 1
|
||||
edge = intervals[i][1];
|
||||
}
|
||||
}
|
||||
return count;
|
||||
return intervals.length - count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user