Simple Java code using Stack


  • 0
    U
        public int findMinArrowShots(int[][] points) {
            // sort points by the int[0];
            Arrays.sort(points, new Comparator<int[]>() {
                public int compare(int[] p1, int[] p2) {
                    return p1[0] - p2[0];
                }
            });
            
            Stack<int[]> stack = new Stack<>();
            
            for (int[] p : points) {
                if (stack.isEmpty()) {
                    stack.push(p);
                }
                int[] old = stack.pop();
                if (old[1] >= p[0]) {
                    stack.push(new int[]{p[0], Math.min(old[1], p[1])});
                } else {
                    stack.push(old);
                    stack.push(p);
                }
            }
            
            return stack.size();
        }
    

Log in to reply
 

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