Easy Java Greedy Solution


  • 0
    J

    The idea is very easy:

    1. sort the array.
    2. find the "shortest" common overlapping interval of balloons (if possible) where shoots an arrow.
    3. repeat step 2 until traverse all balloons.
    public int findMinArrowShots(int[][] points) {
            if(points.length <= 1)  return points.length;
            Arrays.sort(points, (a, b) -> a[0] == b[0]?  a[1]-b[1] : a[0]-b[0]);
            int count = 1;
            int minEnd = points[0][1];
            for(int i = 1; i < points.length; i++) {
                if(points[i][0] <= minEnd) {
                    minEnd = Math.min(minEnd, points[i][1]);
                    continue;
                }
                count++;
                minEnd = points[i][1];
            }
            return count;
        }
    

Log in to reply
 

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