当前位置:网站首页>【Leetcode-每日一题】安装栅栏
【Leetcode-每日一题】安装栅栏
2022-04-23 15:25:00 【牡蛎给芝加哥带来了海】
安装栅栏
难度:困难


代码如下:
class Solution {
int[] subtraction(int[] a, int[] b) {
// 向量相减
return new int[]{
a[0] - b[0], a[1] - b[1]};
}
double cross(int[] a, int[] b) {
// 叉乘
return a[0] * b[1] - a[1] * b[0];
}
double getArea(int[] a, int[] b, int[] c) {
// 向量 ab 转为 向量 ac 过程中扫过的面积
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; // 不标记起点
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; // 非必须
tp--;
}
else break;
}
stk[++tp] = i;
// vis[i] = true; // 非必须
}
int[][] ans = new int[tp - 1][2];
for (int i = 1; i < tp; i++) ans[i - 1] = trees[stk[i]];
return ans;
}
}

版权声明
本文为[牡蛎给芝加哥带来了海]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39143263/article/details/124360211
边栏推荐
- A series of problems about the best time to buy and sell stocks
- Mysql database explanation (IX)
- API gateway / API gateway (II) - use of Kong - load balancing
- Share 20 tips for ES6 that should not be missed
- Functions (Part I)
- What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
- js——實現點擊複制功能
- js——实现点击复制功能
- Practice of unified storage technology of oppo data Lake
- API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
猜你喜欢

About UDP receiving ICMP port unreachable

Thinkphp5 + data large screen display effect

UML学习_day2

MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
![Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]](/img/07/c534238c2b5405bbe4655e51cfee51.png)
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
![Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]](/img/d4/9ee62772b42fa77dfd68a41bde1371.png)
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]

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

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

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

Tencent has written a few words, Ali has written them all for a month
随机推荐
Three uses of kprobe
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
The difference between having and where in SQL
C语言超全学习路线(收藏让你少走弯路)
SSH connects to the remote host through the springboard machine
How to design a good API interface?
Mysql database explanation (8)
Tun equipment principle
8.3 language model and data set
填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
小红书 timestamp2 (2022/04/22)
Introduction to dirty reading, unrepeatable reading and phantom reading
fatal error: torch/extension.h: No such file or directory
Krpano panorama vtour folder and tour
Advanced version of array simulation queue - ring queue (real queuing)
自主作业智慧农场创新论坛
MySQL installation process (steps for successful installation)
Byte interview programming question: the minimum number of K
让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
买卖股票的最佳时机系列问题