当前位置:网站首页>[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
边栏推荐
- HJ31 单词倒排
- adobe illustrator 菜單中英文對照
- MySQL InnoDB transaction
- Redis master-slave synchronization
- Node.js ODBC连接PostgreSQL
- Three uses of kprobe
- Tun model of flannel principle
- Will golang share data with fragment append
- Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
- Redis cluster principle
猜你喜欢

Five data types of redis

My raspberry PI zero 2W toss notes to record some problems and solutions

Machine learning - logistic regression

Set onedrive or Google drive as a drawing bed in upic for free

MySQL InnoDB transaction

How to design a good API interface?

My raspberry PI zero 2W tossing notes record some problems encountered and solutions

推荐搜索 常用评价指标

On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)

How to use OCR in 5 minutes
随机推荐
Llvm - generate local variables
fatal error: torch/extension.h: No such file or directory
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
Have you learned the basic operation of circular queue?
MySQL InnoDB transaction
Explanation of redis database (III) redis data type
Node.js ODBC连接PostgreSQL
Will golang share data with fragment append
Openstack command operation
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Explanation of redis database (I)
8.5 concise implementation of cyclic neural network
买卖股票的最佳时机系列问题
Mysql连接查询详解
重定向和请求转发详解
Set onedrive or Google drive as a drawing bed in upic for free
Rsync + inotify remote synchronization
Redis master-slave synchronization
JUC learning record (2022.4.22)
C language super complete learning route (collection allows you to avoid detours)