nput: [[1,2],[2,3]] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping. ```
Note:
You may assume the interval's end point is always bigger than its start point. Intervals like `[1,2]` and `[2,3]` have borders "touching" but they don't overlap each other.
### Solution:
The idea is to pick the interval with the earliest end time. Then we can get the maximal number of non-overlapping intervals. (or minimal number to remove). This is because, the interval with the earliest end time produces the maximal capacity to hold rest intervals. E.g. Suppose current earliest end time of the rest intervals is x. Then available time slot left for other intervals is `[x:]`. If we choose another interval with end time y, then available time slot would be `[y:]`. Since `x ≤ y`, there is no way `[y:]` can hold more intervals then `[x:]`. Thus, the heuristic holds.
Therefore, we can sort interval by ending time and key track of current earliest end time. Once next interval's start time is earlier than current end time, then we have to remove one interval. Otherwise, we update earliest end time.
```python class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: end, res = float('-inf'), 0 for x in sorted(intervals, key= lambda x: x[1]): if x[0] >= end: end = x[1] else: res += 1 return res
time complexity: $O(nlogn)$, sorting space complexity: $O(1)$ reference: related problem: