LeetCode:用最少的箭引爆气球【452】

LeetCode:用最少的箭引爆气球【452】

题目描述

在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了。开始坐标总是小于结束坐标。平面内最多存在104个气球。

一支弓箭可以沿着x轴从不同点完全垂直地射出。在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足  xstart ≤ x ≤ xend,则该气球会被引爆可以射出的弓箭的数量没有限制。 弓箭一旦被射出之后,可以无限地前进。我们想找到使得所有气球全部被引爆,所需的弓箭的最小数量。

Example:

输入:
[[10,16], [2,8], [1,6], [7,12]]

输出:
2

解释:
对于该样例,我们可以在x = 6(射爆[2,8],[1,6]两个气球)和 x = 11(射爆另外两个气球)。

题目分析

Java题解

class Solution {
    public int findMinArrowShots(int[][] points) {
        if(points.length==0)
            return 0;
        Ballon[] ballons = new Ballon[points.length];
        for(int i=0;i<points.length;i++)
            ballons[i]=new Ballon(points[i][0],points[i][1]);

        Arrays.sort(ballons, new Comparator<Ballon>() {
            @Override
            public int compare(Ballon o1, Ballon o2) {
                return o1.end-o2.end;
            }
        });
        int ans = 1;
        int right = ballons[0].end;
        for(Ballon ballon:ballons)
        {
            if(ballon.start>right)
            {
                right=ballon.end;
                ans++;
            }
        }
        return ans;

    }
}

class Ballon{
    int start;
    int end;

    public Ballon(int start, int end) {
        this.start = start;
        this.end = end;
    }
}

原文地址:https://www.cnblogs.com/MrSaver/p/9499139.html

时间: 2024-07-30 12:57:39

LeetCode:用最少的箭引爆气球【452】的相关文章

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

452. 用最少数量的箭引爆气球 题目描述 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面内最多存在104个气球. 一支弓箭可以沿着x轴从不同点完全垂直地射出.在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足 xstart ≤ x ≤ xend,则该气球会被引爆.可以射出的弓箭的数量没有限制. 弓箭一

LeetCode | 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球【Python】

LeetCode 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球[Medium][Python][区间贪心] Problem LeetCode There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of

力扣第452题 用最少数量的箭引爆气球

力扣第452题 用最少数量的箭引爆气球 class Solution { public: int findMinArrowShots(vector<vector<int>>& points) { int len = points.size(); if (len == 0) { return 0; } sort(points.begin(), points.end(), [](const vector<int> &pl1, const vector<

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

class Solution { public: int findMinArrowShots(vector<pair<int, int>>& points) { if(points.size()==0) return 0; sort(points.begin(), points.end(), [](const auto a, const auto b){ return a.first < b.first; }); int short_num = 1; int shor

[LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

LeetCode:最少移动次数使得数组元素相等||【462】

LeetCode:最少移动次数使得数组元素相等||[462] 题目描述 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只有两个动作是必要的(记得每一步仅可使其中一个元素加1或减1): [1,2,3] => [2,2,3] => [2,2,2] 题目分析 一个直观的理解是这样的,如果我们只有两个数字的话,那么我们使得他们变成相等元素的最少步数是多

LeetCode 312. Burst Balloons(戳气球)

参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/article/details/51208865 public int maxCoins(int[] nums) { int[] balls = new int[nums.length+2]; balls[0] = 1; balls[balls.length - 1] = 1; int[][] coins

Leetcode 462.最少移动次数使数组元素相等

最少移动次数使数组元素相等 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只有两个动作是必要的(记得每一步仅可使其中一个元素加1或减1): [1,2,3] => [2,2,3] => [2,2,2] 排序之后,从两边往中间走,最大和最小之间的差距,是一定要填补上的,不管+1 还是 -1,所以最后都等于中位数. 1 import java.uti

数字问题-LeetCode 452、453、454、455、456、459(KMP算法)

LeetCode # 452 453 454 455 456 459 1 编程题 [LeetCode #452]用最少数量的箭引爆气球 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面内最多存在104个气球. 一支弓箭可以沿着x轴从不同点完全垂直地射出.在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足