当前位置:网站首页>[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
边栏推荐
- YML references other variables
- 2022年中国数字科技专题分析
- Detailed analysis of SQL combat of Niuke database (26-30)
- Subnet division of flannel principle
- How to design a good API interface?
- el-tree实现只显示某一级复选框且单选
- Set onedrive or Google drive as a drawing bed in upic for free
- Mysql database explanation (10)
- 调度系统使用注意事项
- Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
猜你喜欢
Basic operation of sequential stack
Differential privacy (background)
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
Set onedrive or Google drive as a drawing bed in upic for free
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Share 20 tips for ES6 that should not be missed
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
Kubernetes详解(九)——资源配置清单创建Pod实战
Basic operation of circular queue (Experiment)
Detailed explanation of MySQL connection query
随机推荐
码住收藏▏软件测试报告模板范文来了
Llvm - generate addition
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
【thymeleaf】处理空值和使用安全操作符
Mysql database explanation (10)
通过 PDO ODBC 将 PHP 连接到 MSSQL
C language super complete learning route (collection allows you to avoid detours)
Advanced version of array simulation queue - ring queue (real queuing)
adobe illustrator 菜单中英文对照
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
JS -- realize click Copy function
The win10 taskbar notification area icon is missing
Common interview questions of operating system:
A series of problems about the best time to buy and sell stocks
Rsync + inotify remote synchronization
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Share 3 tools, edit 5 works at home and earn more than 400
API gateway / API gateway (II) - use of Kong - load balancing
重定向和请求转发详解
深度学习调参的技巧