Problem description:

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Example 1:

1
2
3
Input: [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.

Example 2:

1
2
3
Input: [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.

Example 3:

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
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: