mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0435.无重叠区间.md
Java,按左边排序,不管右边顺序。相交的时候取最小的右边。
This commit is contained in:
@ -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
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user