452. Minimum Number of Arrows to Burst Balloons
Problem:
先假设在E1的时候打了一枪。需要需要补枪,其实就是判断后续interval和第一个interval是否重叠。用当前的start和初始的end作比较。如果重叠,继续向下走。如果不重叠,如上图走到S3,那么需要再打一枪,同时更新end。
Solution:
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
Analysis:
Solution:
class Solution { public int findMinArrowShots(int[][] points) { if (points == null || points.length == 0) return 0; Arrays.sort(points, (a, b) -> a[1] - b[1]); int res = 1, end = points[0][1]; for (int i = 1; i < points.length; i++) { if (points[i][0] <= end ) { continue; } res++; end = points[i][1]; } return res; } }

评论
发表评论