-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjutant_indices.go
73 lines (61 loc) · 1.29 KB
/
adjutant_indices.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// you can also use imports, for example:
import (
// "fmt"
"math"
"sort"
)
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func Solution(A []int) int {
minDis := make([]int, 0)
// i != i+1
// no value should be present [i] > [i+1] || [i] < [i+1]
lenA := len(A)
for i := 0; i < lenA; i++ {
if i > 1 {
for k := i - 1; k < lenA; k-- {
if A[i] != A[k] {
if !isValueLiesBW(A[i], A[k], A) {
// fmt.Printf("I:%d IV: %d == k:%d kV:%d\n", i, A[i], k, A[k])
d := math.Abs(float64(i) - float64(k))
minDis = append(minDis, int(d))
break
}
}
}
}
for j := i + 1; j < lenA; j++ {
if A[i] != A[j] {
if !isValueLiesBW(A[i], A[j], A) {
// fmt.Printf("I:%d IV: %d == J:%d JV:%d\n", i, A[i], j, A[j])
d := math.Abs(float64(i) - float64(j))
minDis = append(minDis, int(d))
break
}
}
}
}
sort.Ints(minDis)
// fmt.Println("minDis>",minDis)
minDistance := -1
if len(minDis) > 0 {
minDistance = minDis[0]
}
return minDistance
}
func isValueLiesBW(a, b int, arr []int) (present bool) {
var min, max int
if a > b {
min, max = b, a
} else {
min, max = a, b
}
for _, a := range arr {
if a > min && a < max {
present = true
break
}
}
return
}