Skip to content

Commit

Permalink
update 0452.用最少数量的箭引爆气球: 优化 go 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
juguagua authored Dec 16, 2022
1 parent c8b5ca8 commit 6f7f15b
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions problems/0452.用最少数量的箭引爆气球.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

如果真实的模拟射气球的过程,应该射一个,气球数组就remove一个元素,这样最直观,毕竟气球被射了。

但仔细思考一下就发现:如果把气球排序之后,从前到后遍历气球,被射过的气球仅仅跳过就行了,没有必要让气球数组remote气球,只要记录一下箭的数量就可以了。
但仔细思考一下就发现:如果把气球排序之后,从前到后遍历气球,被射过的气球仅仅跳过就行了,没有必要让气球数组remove气球,只要记录一下箭的数量就可以了。

以上为思考过程,已经确定下来使用贪心了,那么开始解题。

Expand Down Expand Up @@ -175,25 +175,25 @@ class Solution:
```

### Go
```golang
```go
func findMinArrowShots(points [][]int) int {
var res int =1//弓箭数
var res int = 1 //弓箭数
//先按照第一位排序
sort.Slice(points,func (i,j int) bool{
return points[i][0]<points[j][0]
sort.Slice(points, func (i,j int) bool {
return points[i][0] < points[j][0]
})

for i:=1;i<len(points);i++{
if points[i-1][1]<points[i][0]{//如果前一位的右边界小于后一位的左边界,则一定不重合
for i := 1; i < len(points); i++ {
if points[i-1][1] < points[i][0] { //如果前一位的右边界小于后一位的左边界,则一定不重合
res++
}else{
} else {
points[i][1] = min(points[i - 1][1], points[i][1]); // 更新重叠气球最小右边界,覆盖该位置的值,留到下一步使用
}
}
return res
}
func min(a,b int) int{
if a>b{
func min(a, b int) int {
if a > b {
return b
}
return a
Expand Down

0 comments on commit 6f7f15b

Please sign in to comment.