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
|
package main
import (
"fmt"
)
func lastVisitor(arr []int) []int {
res, seen := []int{}, []int{}
k := 0
for _, e := range arr {
if e != -1 {
seen = append(seen, e)
k = len(seen)
} else if k == 0 {
res = append(res, -1)
} else {
k--
res = append(res, seen[k])
}
}
return res
}
func main() {
fmt.Println(lastVisitor([]int{5, -1, -1}))
fmt.Println(lastVisitor([]int{3, 7, -1, -1, -1}))
fmt.Println(lastVisitor([]int{2, -1, 4, -1, -1}))
fmt.Println(lastVisitor([]int{10, 20, -1, 30, -1, -1}))
fmt.Println(lastVisitor([]int{-1, -1, 5, -1}))
}
|