Update 0435.无重叠区间.md

Java,按左边排序,不管右边顺序。相交的时候取最小的右边。
This commit is contained in:
ylzou
2021-07-27 16:37:17 +01:00
committed by GitHub
parent 03eda91369
commit 877c8ca418

View File

@ -211,6 +211,29 @@ class Solution {
} }
``` ```
Java:
按左边排序,不管右边顺序。相交的时候取最小的右边。
```java
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals,(a,b)->{
return Integer.compare(a[0],b[0]);
});
int remove = 0;
int pre = intervals[0][1];
for(int i=1;i<intervals.length;i++){
if(pre>intervals[i][0]) {
remove++;
pre = Math.min(pre,intervals[i][1]);
}
else pre = intervals[i][1];
}
return remove;
}
}
```
Python Python
```python ```python
class Solution: class Solution: