package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
 */
public class Problem452 {
    public int findMinArrowShots(int[][] points) {
        if (points.length == 0) {
            return 0;
        }
        Arrays.sort(points, (a, b) -> {
            int comp = Integer.compare(a[0], b[0]);
            if (comp == 0) {
                return Integer.compare(a[1], b[1]);
            }
            return comp;
        });
        for (int i = 0; i < points.length; i++) {
            System.out.println(points[i][0] + ":" + points[i][1]);
        }
        int end1 = points[0][1];
        int result = 1;
        for (int i = 1; i < points.length; i++) {
            int start2 = points[i][0];
            int end2 = points[i][1];
            if (start2 > end1) {
                result++;
                end1 = end2;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem452 prob = new Problem452();
//        System.out.println(prob.findMinArrowShots(new int[][]{{10, 16}, {2, 8}, {1, 6}, {7, 12}})); // 2
//        System.out.println(prob.findMinArrowShots(new int[][]{{10, 16}, {2, 8}, {1, 6}, {7, 9}})); // 3
//        System.out.println(prob.findMinArrowShots(new int[][]{{1, 6}, {7, 8}, {9, 10}})); // 3
//        System.out.println(prob.findMinArrowShots(new int[][]{{1, 6}, {7, 8}, {8, 10}})); // 2
//        System.out.println(prob.findMinArrowShots(new int[][]{{1, 6}})); // 1
//        System.out.println(prob.findMinArrowShots(new int[][]{})); // 0
//        System.out.println(prob.findMinArrowShots(new int[][]{{3, 9}, {7, 12}, {3, 8}, {6, 8}, {9, 10}, {2, 9}, {0, 9}, {3, 9}, {0, 6}, {2, 8}})); // 2
        System.out.println(prob.findMinArrowShots(new int[][]{{9, 12}, {1, 10}, {4, 11}, {8, 12}, {3, 9}, {6, 9}, {6, 7}})); // 2
    }
}

@@ -502,109 +502,28 @@
     
-for (int i = 0; i %3C points.length; i++) %7B%0A            System.out.println(points%5Bi%5D%5B0%5D + %22:%22 +
+int start1 =
  points%5B
 i%5D%5B1
@@ -522,25 +522,14 @@
 nts%5B
-i%5D%5B1%5D);%0A        %7D
+0%5D%5B0%5D;
 %0A   
@@ -733,88 +733,40 @@
 tart
-2 %3E end1) %7B%0A                result++;%0A                end1 = end2;%0A            %7D
+1 %3C= start2 && start2 %3C= end1) %7B
 %0A   
@@ -770,26 +770,24 @@
         
-%7D%0A
         
 return r
@@ -782,963 +782,243 @@
     
-return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem452 prob = new Problem452();%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B10, 16%7D, %7B2, 8%7D, %7B1, 6%7D, %7B7, 12%7D%7D)); // 2%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B10, 16%7D, %7B2, 8%7D, %7B1, 6%7D, %7B7, 9%7D%7D)); // 3%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B1, 6%7D, %7B7, 8%7D, %7B9, 10%7D%7D)); // 3%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B1, 6%7D, %7B7, 8%7D, %7B8, 10%7D%7D)); // 2%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B1, 6%7D%7D)); // 1%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7D)); // 0%0A//        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B3, 9%7D, %7B7, 12%7D, %7B3, 8%7D, %7B6, 8%7D, %7B9, 10%7D, %7B2, 9%7D, %7B0, 9%7D, %7B3, 9%7D, %7B0, 6%7D, %7B2, 8%7D%7D)); // 2%0A        System.out.println(prob.findMinArrowShots(new int%5B%5D%5B%5D%7B%7B9, 12%7D, %7B1, 10%7D, %7B4, 11%7D, %7B8, 12%7D, %7B3, 9%7D, %7B6, 9%7D, %7B6, 7%7D%7D)); // 2
+start1 = Math.max(start1, start2);%0A                end1 = Math.min(end1, end2);%0A            %7D else %7B%0A                result++;%0A                start1 = start2;%0A                end1 = end2;%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 452