-
Notifications
You must be signed in to change notification settings - Fork 481
/
1345.go
51 lines (45 loc) · 1.17 KB
/
1345.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
func minJumps(arr []int) int {
g := make(map[int][]int)
values := make(map[int]bool)
n, step := len(arr), 0
for i := 0; i < n; i++ {
if g[arr[i]] == nil {
g[arr[i]] = []int{}
}
g[arr[i]] = append(g[arr[i]], i)
}
vis := make([]bool, n)
vis[0] = true
q := []int{0}
for len(q) > 0 {
k := len(q)
for k > 0 {
k--
pre := q[0]
q = q[1:]
if pre == n - 1 {
return step
}
if pre - 1 >= 0 && !vis[pre - 1] {
vis[pre - 1] = true
q = append(q, pre - 1)
}
if pre + 1 < n && !vis[pre + 1] {
vis[pre + 1] = true
q = append(q, pre + 1)
}
if _, ok := values[arr[pre]]; ok {
continue
}
values[arr[pre]] = true
for _, i := range g[arr[pre]] {
if !vis[i] {
vis[i] = true
q = append(q, i)
}
}
}
step++
}
return step
}