Given an array of intervals `intervals` where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the remaining intervals non-overlapping. Two intervals [a, b] and [c, d] overlap if they share any point in their interior (i.e., a < d and c < b). Intervals that only touch at an endpoint like [1, 2] and [2, 3] do NOT overlap. For example, given intervals = [[1,2],[2,3],[3,4],[1,3]], you need to remove [1,3] to make the rest non-overlapping, so the answer is 1.
Example 1
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] overlaps both [1,2] and [2,3]. Removing just [1,3] leaves [1,2], [2,3] and [3,4], which only touch at their endpoints.
Example 2
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: The three intervals are identical, so only one can stay. Removing the other two leaves a single [1,2].
Example 3
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: [1,2] and [2,3] only touch at the point 2, which is not an overlap, so nothing needs to be removed.
The classic activity selection insight: always keep the interval that ends earliest, because it leaves the most room for future intervals.
Run checks all cases above. Submit evaluates all test cases.