Input:
[ [1,2], [2,3] ]
Output:
0
Explanation:
You don't need to remove any of the intervals since they're already non-overlapping.
class Solution {
public int eraseOverlapIntervals(Interval[] nums) {
Arrays.sort(nums, (a, b) -> a.start - b.start);
int res = 0;
int last = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i].start < nums[last].end) { //overlapping condition
res++;
if (nums[i].end < nums[last].end) {
last = i; //keep the smaller end
}
} else {
last = i; //cache non-overlapping last
}
}
return res;
}
}