-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
39 lines (35 loc) · 951 Bytes
/
main.go
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
37
38
39
package main
import (
"fmt"
"sort"
)
func eraseOverlapIntervals(intervals [][]int) int {
res := 0
sort.Slice(intervals, func(i, j int) bool {
if intervals[i][0] == intervals[j][0] {
return intervals[i][1] < intervals[j][1]
} else {
return intervals[i][0] < intervals[j][0]
}
})
for i := 1; i < len(intervals); i++ {
if intervals[i][0] < intervals[i-1][1] {
res++
intervals[i][1] = min(intervals[i][1], intervals[i-1][1])
}
}
return res
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func main() {
fmt.Println(eraseOverlapIntervals([][]int{{1, 2}, {2, 3}, {3, 4}, {1, 3}})) // 1
fmt.Println(eraseOverlapIntervals([][]int{{1, 2}, {1, 2}, {1, 2}})) // 2
fmt.Println(eraseOverlapIntervals([][]int{{1, 2}, {2, 3}})) // 0
fmt.Println(eraseOverlapIntervals([][]int{{1, 100}, {11, 22}, {1, 11}, {2, 12}})) // 2
fmt.Println(eraseOverlapIntervals([][]int{{0, 2}, {1, 3}, {2, 4}, {3, 5}, {4, 6}})) // 2
}