|
| 1 | +/* |
| 2 | +
|
| 3 | +*- 452. Minimum Number of Arrows to Burst Balloons -* |
| 4 | +
|
| 5 | +
|
| 6 | +
|
| 7 | +There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [x-start, x-end] denotes a balloon whose horizontal diameter stretches between x-start and x-end. You do not know the exact y-coordinates of the balloons. |
| 8 | +
|
| 9 | +Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with x-start and x-end is burst by an arrow shot at x if x-start <= x <= x-end. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path. |
| 10 | +
|
| 11 | +Given the array points, return the minimum number of arrows that must be shot to burst all balloons. |
| 12 | +
|
| 13 | + |
| 14 | +
|
| 15 | +Example 1: |
| 16 | +
|
| 17 | +Input: points = [[10,16],[2,8],[1,6],[7,12]] |
| 18 | +Output: 2 |
| 19 | +Explanation: The balloons can be burst by 2 arrows: |
| 20 | +- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6]. |
| 21 | +- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12]. |
| 22 | +Example 2: |
| 23 | +
|
| 24 | +Input: points = [[1,2],[3,4],[5,6],[7,8]] |
| 25 | +Output: 4 |
| 26 | +Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows. |
| 27 | +Example 3: |
| 28 | +
|
| 29 | +Input: points = [[1,2],[2,3],[3,4],[4,5]] |
| 30 | +Output: 2 |
| 31 | +Explanation: The balloons can be burst by 2 arrows: |
| 32 | +- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3]. |
| 33 | +- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5]. |
| 34 | + |
| 35 | +
|
| 36 | +Constraints: |
| 37 | +
|
| 38 | +1 <= points.length <= 105 |
| 39 | +points[i].length == 2 |
| 40 | +-231 <= x-start < x-end <= 231 - 1 |
| 41 | +
|
| 42 | +
|
| 43 | +*/ |
| 44 | + |
| 45 | +import 'dart:math'; |
| 46 | + |
| 47 | +/* |
| 48 | +
|
| 49 | +
|
| 50 | +Idea: |
| 51 | +We know that eventually we have to shoot down every balloon, so for each ballon there must be an arrow whose position is between balloon[0] and balloon[1] inclusively. Given that, we can sort the array of balloons by their ending position. Then we make sure that while we take care of each balloon in order, we can shoot as many following balloons as possible. |
| 52 | +
|
| 53 | +So what position should we pick each time? We should shoot as to the right as possible, because since balloons are sorted, this gives you the best chance to take down more balloons. Therefore the position should always be balloon[i][1] for the ith balloon. |
| 54 | +
|
| 55 | +This is exactly what I do in the for loop: check how many balloons I can shoot down with one shot aiming at the ending position of the current balloon. Then I skip all these balloons and start again from the next one (or the leftmost remaining one) that needs another arrow. |
| 56 | +
|
| 57 | +Example: |
| 58 | +
|
| 59 | +balloons = [[7,10], [1,5], [3,6], [2,4], [1,4]] |
| 60 | +After sorting, it becomes: |
| 61 | +
|
| 62 | +balloons = [[2,4], [1,4], [1,5], [3,6], [7,10]] |
| 63 | +So first of all, we shoot at position 4, we go through the array and see that all first 4 balloons can be taken care of by this single shot. Then we need another shot for one last balloon. So the result should be 2. |
| 64 | +
|
| 65 | +
|
| 66 | +*/ |
| 67 | + |
| 68 | +class A { |
| 69 | +/** |
| 70 | +Two key Ideas : |
| 71 | + (a) Greedy heuristic : burst all the balloons whose start is <= min(end); |
| 72 | + (b) If the start > min(end), you need another arrow to burst it so increment arrows and move the end forward. |
| 73 | +**/ |
| 74 | + int findMinArrowShots(List<List<int>> points) { |
| 75 | + if (points.length == 0) return 0; |
| 76 | + points.sort((a, b) => a[1] - b[1]); |
| 77 | + int arrowPos = points[0][1]; |
| 78 | + int arrowCnt = 1; |
| 79 | + for (int i = 1; i < points.length; i++) { |
| 80 | + if (arrowPos >= points[i][0]) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + arrowCnt++; |
| 84 | + arrowPos = points[i][1]; |
| 85 | + } |
| 86 | + return arrowCnt; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class B { |
| 91 | + int findMinArrowShots(List<List<int>> points) { |
| 92 | + // corner case |
| 93 | + if (points.length == 0 || points[0].length == 0) return 0; |
| 94 | + // sort and get merger point |
| 95 | + points.sort((List<int> a, List<int> b) { |
| 96 | + if (a[0] != b[0]) { |
| 97 | + return a[0] - b[0]; |
| 98 | + } else { |
| 99 | + return a[1] - b[1]; |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + List<List<int>> res = []; |
| 104 | + for (List<int> mid in points) { |
| 105 | + //mid[0] and mid[1] |
| 106 | + if (res.length == 0) { |
| 107 | + res.add(mid); |
| 108 | + } else { |
| 109 | + List<int> temp = res[res.length - 1]; |
| 110 | + if (mid[0] <= temp[1]) { |
| 111 | + // value equal is represent interact each other |
| 112 | + temp[0] = max(temp[0], mid[0]); |
| 113 | + temp[1] = min(temp[1], mid[1]); |
| 114 | + res[res.length - 1] = temp; |
| 115 | + } else { |
| 116 | + res.add(mid); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return res.length; |
| 121 | + } |
| 122 | +} |
0 commit comments