当前位置:网站首页>[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
边栏推荐
- 买卖股票的最佳时机系列问题
- T2 iCloud日历无法同步
- Explanation 2 of redis database (redis high availability, persistence and performance management)
- 如何设计一个良好的API接口?
- JUC learning record (2022.4.22)
- Detailed explanation of kubernetes (XI) -- label and label selector
- Openfaas practice 4: template operation
- 【thymeleaf】处理空值和使用安全操作符
- Tencent has written a few words, Ali has written them all for a month
- T2 icloud calendar cannot be synchronized
猜你喜欢

G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction

The win10 taskbar notification area icon is missing

Detailed explanation of redirection and request forwarding

Mysql database explanation (8)

T2 iCloud日历无法同步

Tun equipment principle

What is the role of the full connection layer?

如何设计一个良好的API接口?

MySQL InnoDB transaction

推荐搜索 常用评价指标
随机推荐
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
如何设计一个良好的API接口?
MySQL query library size
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
Set onedrive or Google drive as a drawing bed in upic for free
How to design a good API interface?
通过 PDO ODBC 将 PHP 连接到 MySQL
MySQL installation process (steps for successful installation)
Lotus DB design and Implementation - 1 Basic Concepts
The win10 taskbar notification area icon is missing
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
调度系统使用注意事项
Byte interview programming question: the minimum number of K
js——实现点击复制功能
Redis master-slave synchronization
Share 20 tips for ES6 that should not be missed
JS -- realize click Copy function
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
[thymeleaf] handle null values and use safe operators