-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path452.java
40 lines (38 loc) · 1.18 KB
/
452.java
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
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }
}
class Solution {
public int findMinArrowShots(int[][] points) {
Interval[] intervals = new Interval[points.length];
for (int i = 0; i < points.length; i++) {
Interval in = new Interval(points[i][0],points[i][1]);
intervals[i] = in;
}
Comparator<Interval> comp = new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
return o1.end > o2.end ? 1 : o1.end < o2.end ? -1 : 0;
}
};
Arrays.sort(intervals, comp);
int result = 1;
if (intervals.length == 0) return 0;
Interval active = intervals[0];
for (int i = 1; i < intervals.length; i++) {
Interval cur = intervals[i];
if (cur.start <= active.end) {
// do nothing
} else {
active = intervals[i];
result++;
}
}
return result;
}
}