1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// @Title: 无重叠区间 (Non-overlapping Intervals)
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 8 ms
// @Memory: 4.1 MB
func eraseOverlapIntervals(intervals [][]int) int {
	if len(intervals) == 0 {
		return 0
	}

	// intervals从小到大排序
	sort.Slice(intervals,func(i,j int)bool {
		if intervals[i][0]  == intervals[j][0] {
			return intervals[i][1] < intervals[j][1]
		}
		return intervals[i][0]  <intervals[j][0]
	})

	count := 0
	end := intervals[0][1]

	// for i := range intervals {
		for i := 1;i<len(intervals);i++ {
		if intervals[i][0] >= end {
			end = intervals[i][1]
		} else {
			if intervals[i][1] < end {
				end = intervals[i][1]
			}

			count++
		}
	}

	return count
}