diff --git a/content/posts/leetcode.md b/content/posts/leetcode.md index 817a014..885c48b 100644 --- a/content/posts/leetcode.md +++ b/content/posts/leetcode.md @@ -1463,3 +1463,71 @@ func insert(intervals [][]int, newInterval []int) [][]int { return res } ``` + +## day21 2024-03-18 + +### 452. Minimum Number of Arrows to Burst Balloons + +There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons. + +Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path. + +Given the array points, return the minimum number of arrows that must be shot to burst all balloons. + +![03188j7wiR06A45M](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/03188j7wiR06A45M.png) + +### 题解 + +本题描述的很复杂, 其实是寻找相互交错的数组有几组, 如果将数组的范围看作集合, 范围重叠的数组可以看作一个大集合, 那么就是寻找这样的集合的数目. 本题可用贪心算法, 先将这些数组按照x_start的大小从小到大排序, 然后遍历并寻找x_start在当前重合范围内的数组,并且将重合范围设置为当前重合范围和当前遍历的数组的x_end中的较小值以缩小重合范围. 直到当前数组不满足条件, 即可认为之前的数组需要一个arrow. 继续遍历, 重复该操作, 即可找到所有需要的arrow. + +### 代码 + +```go +func findMinArrowShots(points [][]int) int { + sort.Slice(points, func (i, j int)bool{ + if points[i][0]= points[i][0] {continue} + res++ + arrow = points[i][1] + } + return res +} +```