-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumArrowsToBurstBalloons.java
More file actions
38 lines (26 loc) · 1.07 KB
/
Copy pathMinimumArrowsToBurstBalloons.java
File metadata and controls
38 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package LeetcodePractice;
import java.util.Arrays;
public class MinimumArrowsToBurstBalloons {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) return 0;
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
int arrows = 1;
int arrowPos = points[0][1];
for (int i = 1; i < points.length; i++) {
if (points[i][0] > arrowPos) {
arrows++;
arrowPos = points[i][1];
}
}
return arrows;
}
public static void main(String[] args) {
MinimumArrowsToBurstBalloons solver = new MinimumArrowsToBurstBalloons();
int[][] points1 = {{10,16},{2,8},{1,6},{7,12}};
int[][] points2 = {{1,2},{3,4},{5,6},{7,8}};
int[][] points3 = {{1,2},{2,3},{3,4},{4,5}};
System.out.println("Output 1: " + solver.findMinArrowShots(points1));
System.out.println("Output 2: " + solver.findMinArrowShots(points2));
System.out.println("Output 3: " + solver.findMinArrowShots(points3));
}
}