-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinArrowsShot.java
More file actions
47 lines (40 loc) · 1.17 KB
/
MinArrowsShot.java
File metadata and controls
47 lines (40 loc) · 1.17 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
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/description/
*
* Time complexity: O(nLogn)
* Space complexity: O(n)
*/
public class MinArrowsShot {
public static void main(String[] args) {
int[][] points = { { 10, 16 }, { 2, 8 }, { 1, 6 }, { 7, 12 } };
System.out.println(findMinArrowShots(points));
}
public static int findMinArrowShots(int[][] points) {
int n = points.length;
if (n <= 1)
return n;
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
});
for (int[] p : points) {
queue.add(p);
}
int count = 0;
int[] p = queue.poll();
int end = p[1];
while (!queue.isEmpty()) {
p = queue.poll();
if (p[0] > end) {
count++;
end = p[1];
}
}
return count;
}
}