Skip to content

Latest commit

 

History

History
22 lines (22 loc) · 601 Bytes

452用最少数量的箭引爆气球.md

File metadata and controls

22 lines (22 loc) · 601 Bytes
func findMinArrowShots(points [][]int) int {
    // 以起始点从小到大排序气球
    sort.Slice(points, func(i, j int) bool {
        return points[i][0] < points[j][0]
    })
    // 至少需要一支箭
    count := 1
    for i := 1; i < len(points); i++ {
        // 两个气球不挨着,需要加一支箭
        if points[i][0] > points[i-1][1] {
            count++
        } else {
            // 两个气球挨着,合并气球
            if points[i-1][1] < points[i][1] {
                points[i][1] = points[i-1][1]
            }
        }
    }
    return count
}