LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons

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] = [ x s t a r t , x e n d x_{start}, x_{end} xstart,xend] denotes a balloon whose horizontal diameter stretches between x s t a r t x_{start} xstart and x e n d x_{end} 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 x s t a r t x_{start} xstart and x e n d x_{end} xend is burst by an arrow shot at x if x s t a r t < = x < = x e n d x_{start} <= x <= x_{end} 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.

Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:

Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].

Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].

Example 2:

Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.

Example 3:

Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:

Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].

Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].

Constraints:

  • 1 < = p o i n t s . l e n g t h < = 1 0 5 1 <= points.length <= 10^5 1<=points.length<=105
  • points[i].length == 2
  • − 2 31 < = x s t a r t < x e n d < = 2 31 − 1 -2^{31} <= xstart < xend <= 2^{31} - 1 −231<=xstart<xend<=231−1

From: LeetCode

Link: 452. Minimum Number of Arrows to Burst Balloons


Solution:

Ideas:

1. Problem Analysis:

The problem is essentially asking how many arrows are needed such that each arrow hits at least one balloon, and each balloon is hit by at least one arrow. An important observation here is that if an arrow is shot at some point x, it will burst all balloons whose range covers x.

2. Sorting the Balloons by End Point:

The first key idea in the solution is to sort the balloons by their ending points (i.e., x e n d x_{end} xend). The reasoning behind this is that if we shoot an arrow at the smallest available end point, we ensure that we burst as many balloons as possible that started before this end point.

The compare function helps the qsort function in sorting the balloons based on their end points.

3. Counting Arrows:

After sorting, we initialize our arrow count and set the position of the first arrow to be the end point of the first balloon.

4. Iterating Over the Balloons:

We then iterate over the rest of the balloons. For each balloon, we check its start point:

  • If the start point is less than or equal to the current arrow's position, it means this balloon can be burst by the current arrow, and we move to the next balloon.
  • If the start point is greater than the current arrow's position, it means we need a new arrow. We then increment our arrow count and set the new arrow's position to be the end point of the current balloon.

5. Return the Total Number of Arrows:

After iterating over all balloons, the arrows variable will hold the minimum number of arrows needed to burst all balloons. We return this value.

6. Handling Integer Overflow:

The initial solution had a subtraction in the compare function, which led to integer overflow for large values. We then changed the comparison logic to avoid subtraction, thereby preventing the overflow.

Code:
c 复制代码
int compare(const void* a, const void* b) {
    int end1 = (*(int**)a)[1];
    int end2 = (*(int**)b)[1];
    if (end1 < end2) return -1;
    if (end1 > end2) return 1;
    return 0;
}

int findMinArrowShots(int** points, int pointsSize, int* pointsColSize) {
    if(pointsSize == 0) {
        return 0;
    }

    // Sort the points based on the end values
    qsort(points, pointsSize, sizeof(int*), compare);

    int arrows = 1;
    int arrowPos = points[0][1];

    for(int i = 1; i < pointsSize; i++) {
        // If the start of the balloon is greater than the arrowPos, it means the current arrow can't burst this balloon
        if(points[i][0] > arrowPos) {
            arrows++;
            arrowPos = points[i][1];
        }
    }

    return arrows;
}
相关推荐
passer__jw76712 分钟前
【LeetCode】【算法】283. 移动零
数据结构·算法·leetcode
Ocean☾18 分钟前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序26 分钟前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
TeYiToKu38 分钟前
笔记整理—linux驱动开发部分(9)framebuffer驱动框架
linux·c语言·arm开发·驱动开发·笔记·嵌入式硬件·arm
互联网打工人no11 小时前
每日一题——第一百二十四题
c语言
爱吃生蚝的于勒1 小时前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~1 小时前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
洋2401 小时前
C语言常用标准库函数
c语言·开发语言
王哈哈^_^1 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
星沁城1 小时前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵