力扣第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<int> &pl2){
            return pl1[1] < pl2[1];
        });
        int arrow = 1;
        int endX = points[0][1];
        for (int i = 1; i < len; i++)
        {
            if (points[i][0] > endX)
            {
                arrow++;
                endX = points[i][1];
            }
        }
        return arrow;
    }
};

原文地址:https://www.cnblogs.com/woodjay/p/12387389.html

时间: 2024-08-30 13:34:57

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

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

力扣第70题:爬楼梯

力扣第70题:爬楼梯 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解释: 有两种方法可以爬到楼顶. 1 阶 + 1 阶 2 阶 示例 2: 输入: 3 输出: 3 解释: 有三种方法可以爬到楼顶. 1 阶 + 1 阶 + 1 阶 1 阶 + 2 阶 2 阶 + 1 阶 ? int climbStairs(int n) { if (n <= 1) { re

力扣第95题 不同的二叉搜索树II

力扣第95题 不同的二叉搜索树II 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; vector<TreeNode*> generateTree(int start, int end) { vector<TreeNode*> v

力扣第136题 只出现一次的数

力扣第136题 只出现一次的数 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [2,2,1] 输出: 1 示例 2: 输入: [4,1,2,1,2] 输出: 4 class Solution { public: int singleNumber(vector<int>& nums) { int len = nums.size(); fo

力扣第260题 只出现一次的数字 III

力扣第260题 只出现一次的数字 III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5] 注意: 结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案. 你的算法应该具有线性时间复杂度.你能否仅使用常数空间复杂度来实现? class Solution { public: vector<int> singleNumber(vector<int&

力扣第1013题 将数组分成和相等的三部分

力扣第1013题 将数组分成和相等的三部分 class Solution { public: bool canThreePartsEqualSum(vector<int>& A) { int sum = accumulate(A.begin(), A.end(), 0); if (sum % 3 != 0) return false; int temp = sum / 3; int len = A.size(); int left = 0, right = len - 1; int l