• [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 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).

    用最少数量的箭引爆气球。给一个数组,数组里面存的是类似[left, right]这样的区间,left一定小于right,代表气球直径的起点和终点。求问如果要引爆所有的气球最少需要几只箭。这个题依然用到了扫描线的思想,而且思路基本等同于会议室二。先对所有气球直径的终点排序,然后以第一个气球的终点为起点开始看,如果第二个气球的起点大于第一个气球的终点,就说明两个气球不重叠,则需要另一个箭;同时挪动指针到第二个气球的终点。

    时间O(nlogn) - 对input有排序

    空间O(1)

    JavaScript实现

     1 /**
     2  * @param {number[][]} points
     3  * @return {number}
     4  */
     5 var findMinArrowShots = function (points) {
     6     // corner case
     7     if (points === null || points.length === 0) return 0;
     8 
     9     // normal case
    10     points = points.sort((a, b) => a[1] - b[1]);
    11     let res = 1;
    12     let firstEnd = points[0][1];
    13     for (let i = 1; i < points.length; i++) {
    14         if (points[i][0] > firstEnd) {
    15             res++;
    16             firstEnd = points[i][1];
    17         }
    18     }
    19     return res;
    20 };

    Java实现,注意test case中会给出包含 Integer.MAX_VALUE 和 Integer.MIN_VALUE 的区间,所以只能用 Integer.compare 。单纯的sort会出错。

     1 class Solution {
     2     public int findMinArrowShots(int[][] points) {
     3         // corner case
     4         if (points == null || points.length == 0) {
     5             return 0;
     6         }
     7 
     8         // normal case
     9         // Arrays.sort(points, (a, b) -> a[1] - b[1]);
    10         // Arrays.sort(points, (a, b) -> a[1] < b[1] ? -1 : 1);
    11         Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
    12         int res = 1;
    13         int firstEnd = points[0][1];
    14         for (int i = 1; i < points.length; i++) {
    15             if (points[i][0] > firstEnd) {
    16                 res++;
    17                 firstEnd = points[i][1];
    18             }
    19         }
    20         return res;
    21     }
    22 }

    扫描线相关题目

    LeetCode 题目总结

  • 相关阅读:
    如何在Ubuntu 20.04 LTS Focal Fossa上安装Jekyll
    如何在CentOS 8 / RHEL 8服务器上安装qBittorrent
    如何在Kubernetes中向节点添加或删除标签
    java--io
    hdfs文件上传下载--client模式--Kerberos认证
    java--正则表达式使用
    Javaweb访问Hdfs--Kerberos认证
    Kerberos常用命令
    druid配置详解
    dubbo详细介绍
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12408880.html
Copyright © 2020-2023  润新知