Java easy to understand solution


  • 4
    public class Solution {
        public int findMinArrowShots(int[][] points) {
            if (points == null || points.length == 0)   return 0;
    
            Arrays.sort(points,(a, b) -> a[0] - b[0]);       //sort the balloons according to their start coordinate
            
            int minRight = Integer.MAX_VALUE, count = 0;
            //minRight record the leftmost end of previous balloons
            for (int i = 0; i < points.length; ++i) {
                //whenever current balloon's start is bigger than minRight
                //that means we need an arrow to clear all previous balloons
                if (points[i][0] > minRight) {                   
                    count++;
                    minRight = points[i][1];
                } else {
                    minRight = Math.min(minRight, points[i][1]);
                }
            }
            return count + 1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.