forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (37 loc) · 1.17 KB
/
Solution.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
class Solution {
public int[][] outerTrees(int[][] trees) {
int n = trees.length;
if (n < 4) {
return trees;
}
Arrays.sort(trees, (a, b) -> { return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]; });
boolean[] vis = new boolean[n];
int[] stk = new int[n + 10];
int cnt = 1;
for (int i = 1; i < n; ++i) {
while (cnt > 1 && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) {
vis[stk[--cnt]] = false;
}
vis[i] = true;
stk[cnt++] = i;
}
int m = cnt;
for (int i = n - 1; i >= 0; --i) {
if (vis[i]) {
continue;
}
while (cnt > m && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) {
--cnt;
}
stk[cnt++] = i;
}
int[][] ans = new int[cnt - 1][2];
for (int i = 0; i < ans.length; ++i) {
ans[i] = trees[stk[i]];
}
return ans;
}
private int cross(int[] a, int[] b, int[] c) {
return (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]);
}
}