-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminimum_number_of_arrows_to_burst_balloons.go
73 lines (64 loc) · 1.3 KB
/
minimum_number_of_arrows_to_burst_balloons.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import "sort"
// func findMinArrowShots(points [][]int) int {
// if len(points) == 0 {
// return 0
// }
// sort.Slice(points, func(i, j int) bool {
// return points[i][1] < points[j][1]
// })
// var arrowPosition int = points[0][1]
// var arrowCount int = 1
// for i := 1; i < len(points); i++ {
// if arrowPosition >= points[i][0] {
// continue
// }
// arrowCount++
// arrowPosition = points[i][1]
// }
// return arrowCount
// }
func findMinArrowShots(points [][]int) int {
// corner case
if len(points) == 0 || len(points[0]) == 0 {
return 0
}
// sort and get merger point
sort.Slice(points, func(i, j int) bool {
if points[i][0] != points[j][0] {
return points[i][0] < points[j][0]
} else {
return points[i][1] < points[j][1]
}
})
res := make([][]int, 0)
for _, mid := range points {
//mid[0] and mid[1]
if len(res) == 0 {
res = append(res, mid)
} else {
temp := res[len(res)-1]
if mid[0] <= temp[1] {
// value equal is represent interact each other
temp[0] = max(temp[0], mid[0])
temp[1] = min(temp[1], mid[1])
res[len(res)-1] = temp
} else {
res = append(res, mid)
}
}
}
return len(res)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}