Skip to content

Commit a3a9461

Browse files
author
chen-shiwei
committed
feat: 435、452
1 parent ec13992 commit a3a9461

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package _35_无重叠区间
2+
3+
import "sort"
4+
5+
func eraseOverlapIntervals(intervals [][]int) int {
6+
7+
sort.Slice(intervals, func(i, j int) bool {
8+
return intervals[i][1] < intervals[j][1]
9+
})
10+
l := len(intervals)
11+
result := 1
12+
end := intervals[0][1]
13+
for i := 1; i < l; i++ {
14+
if end <= intervals[i][0] {
15+
result++
16+
end = intervals[i][1]
17+
}
18+
}
19+
return l - result
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package _35_无重叠区间
2+
3+
import "testing"
4+
5+
func Test_eraseOverlapIntervals(t *testing.T) {
6+
type args struct {
7+
intervals [][]int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want int
13+
}{
14+
{name: `输入: [ [1,2], [2,3], [3,4], [1,3] ]
15+
16+
输出: 1`, args: args{intervals: [][]int{{1, 2}, {2, 3}, {3, 4}, {1, 3}}}, want: 1},
17+
}
18+
for _, tt := range tests {
19+
t.Run(tt.name, func(t *testing.T) {
20+
if got := eraseOverlapIntervals(tt.args.intervals); got != tt.want {
21+
t.Errorf("eraseOverlapIntervals() = %v, want %v", got, tt.want)
22+
}
23+
})
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package _52_用最少数量的箭引爆气球
2+
3+
import "sort"
4+
5+
func findMinArrowShots(points [][]int) int {
6+
7+
l := len(points)
8+
sort.Slice(points, func(i, j int) bool {
9+
return points[i][0] < points[j][0]
10+
})
11+
12+
result := 1
13+
for i := 1; i < l; i++ {
14+
// 不沾边
15+
if points[i][0] > points[i-1][1] {
16+
result++
17+
} else {
18+
points[i][1] = min(points[i][1], points[i-1][1])
19+
}
20+
}
21+
22+
return result
23+
}
24+
25+
func min(a, b int) int {
26+
if a < b {
27+
return a
28+
}
29+
return b
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package _52_用最少数量的箭引爆气球
2+
3+
import "testing"
4+
5+
func Test_findMinArrowShots(t *testing.T) {
6+
type args struct {
7+
points [][]int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want int
13+
}{
14+
{name: `输入:points = [[10,16],[2,8],[1,6],[7,12]]
15+
输出:2`, args: args{points: [][]int{{10, 16}, {2, 8}, {1, 6}, {7, 12}}}, want: 2},
16+
}
17+
for _, tt := range tests {
18+
t.Run(tt.name, func(t *testing.T) {
19+
if got := findMinArrowShots(tt.args.points); got != tt.want {
20+
t.Errorf("findMinArrowShots() = %v, want %v", got, tt.want)
21+
}
22+
})
23+
}
24+
}

0 commit comments

Comments
 (0)