Javascript Solution


  • 0
    S
    var eraseOverlapIntervals = function(intervals) {
        if (intervals.length <= 1) {
            return 0;
        }
        
        intervals = intervals.sort(function (x, y) {
            return x.start - y.start;
        });
        
        var count = 0;
        var cursor = 1;
        
        while(cursor < intervals.length) {
            var removeIndex;
            
            if (intervals[cursor].start === intervals[cursor - 1].start || intervals[cursor].start <  intervals[cursor - 1].end) {
                removeIndex = intervals[cursor].end > intervals[cursor - 1].end ? cursor : (cursor - 1);
                
                intervals.splice(removeIndex, 1);
                count++;
                
                continue;
            }
            
            if (intervals[cursor].end <= intervals[cursor - 1].end) {
                removeIndex = intervals[cursor].start >  intervals[cursor - 1].start ? (cursor - 1) : cursor;
             
                intervals.splice(removeIndex, 1);
                count++;
                
                continue;
            }
            
            cursor++;
        }
        
        return count;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.