LeetCode上利用数学、位运算、数学推导解答的题目集合!

452.最少的箭引爆气球

452.最少的箭引爆气球





class Solution {
    //需要按区间尾部升序排序
    public int findMinArrowShots(int[][] points) {
        if(points==null || points.length==0) return 0;
        Arrays.sort(points, new endcomparator());
        int ans = 1;
        int lastEnd = points[0][1];
        for(int i=1;i<points.length;i++){
            if(points[i][0]>lastEnd){
                ans++;
                lastEnd = points[i][1];
            }
        }
        return ans;
    }

    class endcomparator implements Comparator<int[]>{
        public int compare(int[] a, int[] b) {
            return a[1] - b[1];
        }
    }
}

Last modification:April 21st, 2020 at 02:23 am