当前位置:网站首页>[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
边栏推荐
- adobe illustrator 菜單中英文對照
- Tencent has written a few words, Ali has written them all for a month
- Wechat applet customer service access to send and receive messages
- 如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
- Have you really learned the operation of sequence table?
- Llvm - generate if else and pH
- C语言超全学习路线(收藏让你少走弯路)
- 我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
- JUC learning record (2022.4.22)
- Node.js ODBC连接PostgreSQL
猜你喜欢

Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list

asp. Net method of sending mail using mailmessage

Analysis of common storage types and FTP active and passive modes

函数(第一部分)

Tencent has written a few words, Ali has written them all for a month

UML learning_ Day2

Mysql database explanation (8)

Redis master-slave synchronization

X509 certificate cer format to PEM format

Mysql database explanation (IX)
随机推荐
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Nacos程序连接MySQL8.0+ NullPointerException
Rsync + inotify remote synchronization
Leetcode学习计划之动态规划入门day3(198,213,740)
Machine learning - logistic regression
移动app测试如何进行?
C language super complete learning route (collection allows you to avoid detours)
Three uses of kprobe
What is the role of the full connection layer?
【thymeleaf】处理空值和使用安全操作符
YML references other variables
Krpano panorama vtour folder and tour
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
Explanation of redis database (I)
setcontext getcontext makecontext swapcontext
【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
Precautions for use of dispatching system
Explanation of redis database (IV) master-slave replication, sentinel and cluster
My raspberry PI zero 2W toss notes to record some problems and solutions