-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumArrows.java
More file actions
49 lines (39 loc) · 1.19 KB
/
MinimumArrows.java
File metadata and controls
49 lines (39 loc) · 1.19 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
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* Leet code problem #452. Minimum Number of Arrows to Burst Balloons
* https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
*/
public class MinimumArrows {
public static void main(String[] args) {
int[][] points = { { -2147483646, -2147483645 }, { 2147483646, 2147483647 } };
System.out.println(findMinArrowShots(points));
}
public static int findMinArrowShots(int[][] points) {
int n = points.length;
if (n == 0) {
return 0;
}
// Sort by finish time
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return (a[1] > b[1]) ? 1 : -1;
}
});
for (int i = 0; i < n; i++) {
pq.add(points[i]);
}
int arrows_count = 1;
int[] p = pq.poll();
int end = p[1];
while (!pq.isEmpty()) {
p = pq.poll();
if (p[0] > end) {
arrows_count++;
end = p[1];
}
}
return arrows_count;
}
}