435 non overlapping intervals

·data-structure-and-algorithm
#greedy-algorithm

435. 无重叠区间

go:

import "sort"

func eraseOverlapIntervals(intervals [][]int) int {
    if len(intervals) == 0 {
        return 0
    }

    sort.Slice(intervals, func(i, j int) bool {
        return intervals[i][1] < intervals[j][1]
    })

    count, pos := 0, intervals[0][1]
    for i := 1; i < len(intervals); i++ {
        if pos > intervals[i][0] {
            count++
        } else {
            pos = intervals[i][1]
        }
    }

    return count
}