• 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;
        }
    }
    
  • 相关阅读:
    iOS CoreData使用笔记
    iOS UIWebView中添加手势
    Swift sha1 md5加密
    Swift用block响应UIAlertView和UIActionSheet的按钮点击事件
    iOS判断iPhone型号
    iOS Document Interaction 编程指南
    Swift项目开发中缓存计算以及清除
    SVN 一次性提交多个目录中文件
    svn 相关
    sublime text 2相关
  • 原文地址:https://www.cnblogs.com/MrSaver/p/9499139.html
Copyright © 2020-2023  润新知