# 435. Non-overlapping Intervals



{% embed url="https://leetcode.com/problems/non-overlapping-intervals/description/" %}

```javascript
/**
 * @param {number[][]} intervals
 * @return {number}
 */
var eraseOverlapIntervals = function (intervals) {
    intervals.sort((a, b) => a[0] - b[0]);
    const n = intervals.length;

    let cnt = 0;

    let temp = intervals[0];
    for (let i = 1; i < n; i++) {
        const curr = intervals[i];
        
        if (temp[1] <= curr[0]) {
            temp = curr;
        } else if (temp[1] > curr[0]) {
            cnt++;
            temp[0] = curr[0];
            temp[1] = Math.min(temp[1], curr[1]);
        }

    }

    return cnt;
};
```



