当前位置:网站首页>[leetcode daily question] install fence
[leetcode daily question] install fence
2022-04-23 15:26:00 【Oysters brought the sea to Chicago】
Install fence
difficulty : difficult


The code is as follows :
class Solution {
int[] subtraction(int[] a, int[] b) {
// Vector subtraction
return new int[]{
a[0] - b[0], a[1] - b[1]};
}
double cross(int[] a, int[] b) {
// Cross riding
return a[0] * b[1] - a[1] * b[0];
}
double getArea(int[] a, int[] b, int[] c) {
// vector ab To vector ac The area swept in the process
return cross(subtraction(b, a), subtraction(c, a));
}
public int[][] outerTrees(int[][] trees) {
Arrays.sort(trees, (a, b)->{
return a[0] != b[0] ? a[0] - b[0] : a[1] - b[1];
});
int n = trees.length, tp = 0;
int[] stk = new int[n + 10];
boolean[] vis = new boolean[n + 10];
stk[++tp] = 0; // Do not mark the starting point
for (int i = 1; i < n; i++) {
int[] c = trees[i];
while (tp >= 2) {
int[] a = trees[stk[tp - 1]], b = trees[stk[tp]];
if (getArea(a, b, c) < 0) vis[stk[tp--]] = false;
else break;
}
stk[++tp] = i;
vis[i] = true;
}
int size = tp;
for (int i = n - 1; i >= 0; i--) {
if (vis[i]) continue;
int[] c = trees[i];
while (tp > size) {
int[] a = trees[stk[tp - 1]], b = trees[stk[tp]];
if (getArea(a, b, c) < 0) {
// vis[stk[tp--]] = false; // Not necessary
tp--;
}
else break;
}
stk[++tp] = i;
// vis[i] = true; // Not necessary
}
int[][] ans = new int[tp - 1][2];
for (int i = 1; i < tp; i++) ans[i - 1] = trees[stk[i]];
return ans;
}
}

版权声明
本文为[Oysters brought the sea to Chicago]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231524468787.html
边栏推荐
- Share 3 tools, edit 5 works at home and earn more than 400
- MySQL Basics
- Redis cluster principle
- setcontext getcontext makecontext swapcontext
- How to use OCR in 5 minutes
- YML references other variables
- MySQL query library size
- Detailed explanation of MySQL connection query
- 深度学习——超参数设置
- Openstack theoretical knowledge
猜你喜欢
随机推荐
木木一路走好呀
regular expression
Do keyword search, duplicate keyword search, or do not match
Openfaas practice 4: template operation
Mysql database explanation (10)
Leetcode学习计划之动态规划入门day3(198,213,740)
Lotus DB design and Implementation - 1 Basic Concepts
什么是CNAS认证?CNAS认可的软件测评中心有哪些?
GFS distributed file system (Theory)
Compiling OpenSSL
JS -- realize click Copy function
Byte interview programming question: the minimum number of K
JS - implémenter la fonction de copie par clic
The wechat applet optimizes the native request through the promise of ES6
MySQL Basics
Subnet division of flannel principle
通过 PDO ODBC 将 PHP 连接到 MySQL
tcp_ Diag kernel related implementation 1 call hierarchy
深度学习调参的技巧
Mysql连接查询详解








