当前位置:网站首页>2022-02-19: fence installation. In a two-dimensional garden, there are some trees represented by (x, y) coordinates. As the installation cost is very expensive, your task is to enclose all the trees w
2022-02-19: fence installation. In a two-dimensional garden, there are some trees represented by (x, y) coordinates. As the installation cost is very expensive, your task is to enclose all the trees w
2022-06-25 06:13:00 【Fuda scaffold constructor's daily question】
2022-02-19: Install fence .
In a two-dimensional Garden , There are some uses. (x, y) Coordinate tree . Because the installation cost is very expensive , Your task is to surround all the trees with the shortest rope . Only when all the trees are surrounded by ropes , A garden can fence . You need to find the coordinates of the trees right on the fence boundary .
Power button 587.
answer 2022-02-19:
convex hull . Two dimensional coordinate system , From left to right , Sort from bottom to top .
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"sort"
)
func main() {
points := [][]int{
{
1, 1}, {
2, 2}, {
2, 0}, {
2, 4}, {
3, 3}, {
4, 2}}
ret := outerTrees(points)
fmt.Println(ret)
}
func outerTrees(points [][]int) [][]int {
n := len(points)
s := 0
stack := make([][]int, n<<1)
// x In front of the small row ,x Same ,y In front of the small row
sort.Slice(points, func(i, j int) bool {
a := points[i]
b := points[j]
if a[0] != b[0] {
return a[0] < b[0]
} else {
return a[1] < b[1]
}
})
for i := 0; i < n; i++ {
for s > 1 && cross(stack[s-2], stack[s-1], points[i]) > 0 {
s--
}
stack[s] = points[i]
s++
}
for i := n - 2; i >= 0; i-- {
for s > 1 && cross(stack[s-2], stack[s-1], points[i]) > 0 {
s--
}
stack[s] = points[i]
s++
}
// Go back to
//Arrays.sort(stack, 0, s, (a, b) -> b[0] == a[0] ? b[1] - a[1] : b[0] - a[0]);
n = 1
for i := 1; i < s; i++ {
// If i spot ,x and y, And i-1 spot ,x and y Are all the same
// i Point and i-1 spot , In the same place , here ,i Point not reserved
if stack[i][0] != stack[i-1][0] || stack[i][1] != stack[i-1][1] {
stack[n] = stack[i]
n++
}
}
return stack[0:n]
//return Arrays.copyOf(stack, n)
}
// The realization of cross multiplication
// Suppose there is a、b、c Three points , And give the... Of each point (x,y) Location
// from a To c Vector , In from a To b Which side of the vector of ?
// If a To c Vector , In from a To b To the right of the vector , Return positive number
// If a To c Vector , In from a To b To the left of the vector , Return negative
// If a To c Vector , And from the a To b The vectors of coincide , return 0
func cross(a, b, c []int) int {
return (b[1]-a[1])*(c[0]-b[0]) - (b[0]-a[0])*(c[1]-b[1])
}
The results are as follows :
边栏推荐
- Analysis report on global and Chinese pharmaceutical excipients industry competition and marketing model 2022-2028
- Research Report on global and Chinese vaccine market profit forecast and the 14th five year plan development strategy 2022-2028
- [Suanli network] problems and challenges faced by the development of Suanli network
- Noi Mathematics: Dirichlet convolution
- The locally developed SAP ui5 application is deployed to the ABAP server to perform error analysis
- CST8227
- John
- Use of arrays tool class
- Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
- Optimal Parking
猜你喜欢

MySQL tuning -- 02 -- slow query log
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
![[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management](/img/62/dab2ac0526795f2040394acd9efdd3.jpg)
[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management

Pre knowledge of asynchronous operation
[golang] leetcode intermediate - Search rotation sort array & search two-dimensional matrix II
Use generator-easy-ui5 to quickly create the engineering structure of SAP ui5 applications

Tablespace free space

MySQL tuning --01--- optimization steps and system performance parameters

What happens when redis runs out of memory
How SAP ui5 device type detection device API works
随机推荐
Leetcode topic [array] -36- effective Sudoku
[Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security
Ping command – test network connectivity between hosts
Mongodb basic concept learning - set
The simplest way to tell you is to hash and not hash
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
Introduction to MySQL test run test framework
An example of recursion, the full permutation problem of 1~n
How SAP ui5 device type detection device API works
Which securities company is good for opening a mobile account? Is it safe to open a mobile account?
Soft exam information system project manager_ Management Science (Operations Research) -- senior information system project manager of soft test 033
Research Report on demand and Competitive Prospect of global and Chinese welding personal protective equipment industry 2022-2027
SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version
PHP output (print) log to TXT text
Trial version of routing history and routing back and history of SAP ui5
RM command – remove file or directory
DF command – displays disk space usage
Various errors and solutions encountered when deploying SAP ui5 application to ABAP server with SAP Fiori tools
C switch nested syntax
Understanding the dynamic mode of mongodb document