当前位置:网站首页>【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
边栏推荐
- On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
- Have you really learned the operation of sequence table?
- How to design a good API interface?
- Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
- Mysql database explanation (IX)
- What is the role of the full connection layer?
- Three uses of kprobe
- Do keyword search, duplicate keyword search, or do not match
- Mysql database explanation (8)
- Educational Codeforces Round 127 A-E题解
猜你喜欢
T2 icloud calendar cannot be synchronized
What is the role of the full connection layer?
Sword finger offer (1) -- for Huawei
regular expression
How does eolink help telecommuting
My raspberry PI zero 2W toss notes to record some problems and solutions
Byte interview programming question: the minimum number of K
8.2 text preprocessing
For 22 years, you didn't know the file contained vulnerabilities?
Basic operation of circular queue (Experiment)
随机推荐
fatal error: torch/extension.h: No such file or directory
重定向和请求转发详解
Sqlserver transaction and lock problem
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
Llvm - generate if else and pH
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
My raspberry PI zero 2W toss notes to record some problems and solutions
如何设计一个良好的API接口?
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
like和regexp差别
Flink datastream type system typeinformation
小红书 timestamp2 (2022/04/22)
Will golang share data with fragment append
What is the role of the full connection layer?
JUC learning record (2022.4.22)
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Reptile exercises (1)
Three uses of kprobe
Basic operation of circular queue (Experiment)
How does eolink help telecommuting