【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/

题目描述:

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 start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

Example:

Input:
[[10,16], [2,8], [1,6], [7,12]] Output:
2 Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

题目大意

给出了一组气球的起始和结束的坐标,现在用箭去沿着y轴方向射。最少需要多少个箭才能把所有的气球打破?

解题方法

这个题是贪心算法的题目,看到这种问区间重叠情况的,一般都能想到是贪心。我们把所有的区间按照右边界进行排序,因为每个气球都要被打破,因此排序得到的第一组数据我们一定要使用。可以想到,只要沿着该数据最右边界的位置进行射箭一定能打破尽可能多的气球。然后依次移动射箭的位置,进行统计即可。

class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
if not points: return 0
points.sort(key = lambda x : x[1])
curr_pos = points[0][1]
ans = 1
for i in range(len(points)):
if curr_pos >= points[i][0]:
continue
curr_pos = points[i][1]
ans += 1
return ans

日期

2018 年 4 月 10 日 ———— 风很大,很舒服~~

【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  4. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  5. 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. 452. Minimum Number of Arrows to Burst Balloons扎气球的个数最少

    [抄题]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pr ...

  7. [LC] 452. Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. 【leetcode】452. Minimum Number of Arrows to Burst Balloons

    题目如下: 解题思路:本题可以采用贪心算法.首先把balloons数组按end从小到大排序,然后让第一个arrow的值等于第一个元素的end,依次遍历数组,如果arrow不在当前元素的start到en ...

  9. 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球

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

随机推荐

  1. 使用Openmp并行化

    运行命令:g++ -fopenmp xx.cpp -lgomp -lpthread -o xx.out 用例一: #include <omp.h> #include <stdio.h ...

  2. Shell 输出第五行的内容

    目录 Shell 输出第五行的内容 题目 题解-awk 题解-sed Shell 输出第五行的内容 题目 写一个 bash脚本以输出一个文本文件 nowcoder.txt 中第5行的内容. 示例: 假 ...

  3. A Child's History of England.21

    There was one tall Norman Knight who rode before the Norman army on a prancing horse, throwing up hi ...

  4. 零基础学习java------23---------动态代理,ip,url案例

    1. 动态代理 2. ip,url案例 给定的access.log是电信运营商的用户上网数据,第一个字段是时间, 第二个字段是ip地址,第三个字段是访问的网站,其他字段可以忽略不计. 第一个字段是网段 ...

  5. Excel 数据验证:分类选择及输入限制

    几个简单设置让你的数据不再出错 如何快速选择某一大类中的细分小类 多级菜单 注意:引用可以创建二级目录,但是引用前应先用公式定义名称,然后引用,引用只能在本sheet操作.

  6. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  7. Spring Cloud Feign原理详解

    目录 1.什么是Feign? 2.Open Feign vs Spring Cloud Feign 2.1.OpenFeign 2.2.Spring Cloud Open Feign 3.Spring ...

  8. 【Python】【Module】re

    python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配 ...

  9. 【Linux】【Problems】在fedora 9上解决依赖问题

    summary: 在32bit的fedora9上安装EMC客户端遇到无法解决的依赖问题 detail: rpm 安装: [root@hcszmons02 tmp]# rpm -ivh lgtoclnt ...

  10. Reactor之发射器(Flux、Mono)转换操作函数

    数据合并函数 由于业务需求有的时候需要将多个数据源进行合并,Reactor提供了concat方法和merge方法: concat public static <T> Flux<T&g ...