当前位置:网站首页>【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
边栏推荐
- Reptile exercises (1)
- 深度学习——超参数设置
- Krpano panorama vtour folder and tour
- PSYNC synchronization of redis source code analysis
- Llvm - generate if else and pH
- Tencent has written a few words, Ali has written them all for a month
- 免费在upic中设置OneDrive或Google Drive作为图床
- X509 certificate cer format to PEM format
- Hj31 word inversion
- MySQL InnoDB transaction
猜你喜欢
setcontext getcontext makecontext swapcontext
Thinkphp5 + data large screen display effect
MySQL InnoDB transaction
Lotus DB design and Implementation - 1 Basic Concepts
What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
Functions (Part I)
机器学习——逻辑回归
Mysql database explanation (IX)
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
Machine learning - logistic regression
随机推荐
MySQL sync could not find first log file name in binary log index file error
Openstack command operation
[thymeleaf] handle null values and use safe operators
如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
Baidu written test 2022.4.12 + programming topic: simple integer problem
Nacos程序连接MySQL8.0+ NullPointerException
Nuxt project: Global get process Env information
MySQL InnoDB transaction
nuxt项目:全局获取process.env信息
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
Do keyword search, duplicate keyword search, or do not match
分布式事务Seata介绍
Educational Codeforces Round 127 A-E题解
Common interview questions of operating system:
Reptile exercises (1)
群体智能自主作业智慧农场项目启动及实施方案论证会议
Basic operation of circular queue (Experiment)
Deep learning - Super parameter setting
Openfaas practice 4: template operation
Tencent has written a few words, Ali has written them all for a month