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

这篇具有很好参考价值的文章主要介绍了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<=2311

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.文章来源地址https://www.toymoban.com/news/detail-654564.html

Code:
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;
}

到了这里,关于LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解题思路 2. 代码实现 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。 因此,剩下的

    2024年01月20日
    浏览(32)
  • LeetCode447. Number of Boomerangs

    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). Return the number of boomerangs. Example 1: Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs

    2024年02月02日
    浏览(24)
  • LeetCode 933. Number of Recent Calls

    ou have a  RecentCounter  class which counts the number of recent requests within a certain time frame. Implement the  RecentCounter  class: RecentCounter()  Initializes the counter with zero recent requests. int ping(int t)  Adds a new request at time  t , where  t  represents some time in milliseconds, and returns the number of requests that has h

    2024年02月08日
    浏览(36)
  • LeetCode //C - 933. Number of Recent Calls

    You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the pas

    2024年01月23日
    浏览(36)
  • LeetCode每日一题(2457. Minimum Addition to Make Integer Beautiful)

    You are given two positive integers n and target. An integer is considered beautiful if the sum of its digits is less than or equal to target. Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful. Example 1: Input: n = 16, target = 6 Output: 4 Explanation: Init

    2023年04月16日
    浏览(81)
  • LeetCode 1000. Minimum Cost to Merge Stones【记忆化搜索,动态规划,数组】困难

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月26日
    浏览(88)
  • LeetCode 1921. Eliminate Maximum Number of Monsters【贪心,计数排序】1527

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(39)
  • LeetCode2111. Minimum Operations to Make the Array K-Increasing——动态规划

    You are given a 0-indexed array arr consisting of n positive integers, and a positive integer k. The array arr is called K-increasing if arr[i-k] = arr[i] holds for every index i, where k = i = n-1. For example, arr = [4, 1, 5, 2, 6, 2] is K-increasing for k = 2 because: arr[0] = arr[2] (4 = 5) arr[1] = arr[3] (1 = 2) arr[2] = arr[4] (5 = 6) arr[3] = arr[5]

    2024年03月09日
    浏览(32)
  • Java异常 #Number of lines annotated by Git is not equal to number of lines in the file, check file …

    在项目中某个 java 文件左边栏右键查看代码版本履历(Annotate)时无法显示,IDEA 提示:Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators.   这个问题涉及到不同操作系统下文本文件的换行符差异引起的。在不同操作系统中,文本文件的

    2024年02月03日
    浏览(35)
  • LeetCode 2475. Number of Unequal Triplets in Array【数组,排序,哈希表】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包