
Given an array of intervals, determine the minimum number of intervals to remove so that the remaining intervals do not overlap with each other.
Important Note: Two intervals are considered non-overlapping if they only touch at a single point. For example, [1,2] and [2,3] are non-overlapping (they touch at point 2), but [1,3] and [2,4] are overlapping (they share the range [2,3]).
Visual Example:
Original:
[1--2] [2--3] [3--4] [1------3]
✗ Overlaps: [1,3] spans multiple intervals
Optimal removal: Remove [1,3]
[1--2] [2--3] [3--4]
✓ Non-overlapping (touching at boundaries is allowed)
Minimum intervals removed: 1
Constraints:
[start, end], where start < endfunction eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[0] - b[0])
let count = 0
let last = intervals[0]
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < last[1]) {
count++
if (intervals[i][1] < last[1]) {
last = intervals[i];
}
} else {
last = intervals[i]
}
}
return count
};