-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path587.go
More file actions
45 lines (40 loc) · 811 Bytes
/
587.go
File metadata and controls
45 lines (40 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
func outerTrees(trees [][]int) [][]int {
isMoreClockWise := func(a, b, c []int) int {
return (c[0]-a[0])*(b[1]-a[1]) - (c[1]-a[1])*(b[0]-a[0])
}
n := len(trees)
if n < 4 {
return trees
}
// Find most left tree
minLeft := 0
for i, val := range trees {
if val[0] < trees[minLeft][0] {
minLeft = i
}
}
res := make(map[int][]int, 0)
cur := minLeft
for j := 0; j < n; j++ {
cand := (cur + 1) % n
for i := 0; i < n; i++ {
if isMoreClockWise(trees[cur], trees[cand], trees[i]) > 0 {
cand = i
}
}
for i := 0; i < n; i++ {
if isMoreClockWise(trees[cur], trees[cand], trees[i]) == 0 {
res[i] = trees[i]
}
}
cur = cand
if cur == minLeft {
break
}
}
result := make([][]int, 0)
for _, val := range res {
result = append(result, val)
}
return result
}