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

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(射爆另外两个气球)。

贴出代码

class Solution {
    public int findMinArrowShots(int[][] points) {
        if (points.length == 0) return 0;
        Arrays.sort(points, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        int cnt = 1, end = points[0][1];
        for (int i = 1; i < points.length; i ++){
            if (points[i][0] > end){
                cnt ++;
                end = points[i][1];
            }
        }
        return cnt;
    }
}

原文地址:https://www.cnblogs.com/Tu9oh0st/p/10994186.html

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

452. 用最少数量的箭引爆气球的相关文章

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

力扣第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 | 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

[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:用最少的箭引爆气球【452】

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

poj1753-Flip Game(高斯消元枚举xor线性方程自由变元的值,找为1解的最少数量)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31227   Accepted: 13583 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

leet

# 题名1 两数之和    2 两数相加    3 无重复字符的最长子串    4 寻找两个有序数组的中位数    5 最长回文子串    6 Z 字形变换    7 整数反转    8 字符串转换整数 (atoi)    9 回文数    10 正则表达式匹配    11 盛最多水的容器    12 整数转罗马数字    13 罗马数字转整数    14 最长公共前缀    15 三数之和    16 最接近的三数之和    17 电话号码的字母组合    18 四数之和    19 删除链表

数字问题-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, 且满足

[Leetcode 452] 最少需要射出多少支箭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