Non-overlapping Intervals

You are given an array of intervals, where intervals[i] = [start, end].

Return the minimum number of intervals to remove so that no two of the remaining intervals overlap. Intervals that only touch, like [1, 2] and [2, 3], do not overlap.

Constraints

  • 1 <= intervals.length <= 10^5
  • -5 * 10^4 <= start < end <= 5 * 10^4

Examples

Example 1

Input: intervals = [[1,3],[2,4],[3,5],[1,5]]
Output: 2

Keep [1, 3] and [3, 5] (touching at 3 is fine) and remove [2, 4] and [1, 5]. No three of these intervals avoid each other, so 2 removals is the minimum.

Example 2

Input: intervals = [[4,6],[4,6],[4,6],[4,6]]
Output: 3

All four are the same interval, so only one of them can stay.

Example 3

Input: intervals = [[1,2],[2,3]]
Output: 0

The two intervals only touch at 2, which doesn't count as overlapping.

JavaScript you'll use: Sorting and comparators Destructuring, spread and rest

/**
 * @param {number[][]} intervals
 * @return {number}
 */
function eraseOverlapIntervals(intervals) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.