当前位置:网站首页>【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
边栏推荐
- Mysql database explanation (8)
- 如何设计一个良好的API接口?
- How to use OCR in 5 minutes
- Llvm - generate if else and pH
- My raspberry PI zero 2W tossing notes record some problems encountered and solutions
- Openstack theoretical knowledge
- Basic operation of circular queue (Experiment)
- Nuxt project: Global get process Env information
- Differential privacy (background)
- The wechat applet optimizes the native request through the promise of ES6
猜你喜欢

8.4 realization of recurrent neural network from zero

Mysql连接查询详解

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

我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法

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

Openstack command operation

Lotus DB design and Implementation - 1 Basic Concepts

Introduction to distributed transaction Seata

API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)

UML learning_ Day2
随机推荐
Hj31 word inversion
Mysql database explanation (IX)
T2 icloud calendar cannot be synchronized
Grep was unable to redirect to the file
setcontext getcontext makecontext swapcontext
22年了你还不知道文件包含漏洞?
Lotus DB design and Implementation - 1 Basic Concepts
Explanation of redis database (I)
The difference between having and where in SQL
Alexnet model
Differential privacy (background)
Practice of unified storage technology of oppo data Lake
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
Machine learning - logistic regression
Tun model of flannel principle
MySQL InnoDB transaction
How to upload large files quickly?
JS -- realize click Copy function
like和regexp差别
脏读、不可重复读和幻读介绍