Skip to content

Instantly share code, notes, and snippets.

@ccw
Created March 26, 2019 17:17
Show Gist options
  • Save ccw/e741fc617d96936b2d4dbed75bb0aef8 to your computer and use it in GitHub Desktop.
Save ccw/e741fc617d96936b2d4dbed75bb0aef8 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Person struct {
idx int
ts int32
dir int32
}
func getTimes(time []int32, direction []int32) []int32 {
result := make([]int32, len(time))
queue := make([]Person, len(time))
for i, t := range time {
d := direction[i]
queue[i] = Person{
idx: i,
ts: t,
dir: d,
}
}
lt := int32(-1)
ld := int32(1)
for i := 0; i < len(time); i++ {
candidate := queue[0]
if len(queue) > 1 {
td := int32(1)
tf := func (ts int32) bool {
return ts == candidate.ts
}
if candidate.ts - lt <= 1 {
td = ld
tf = func(ts int32) bool {
return ts - lt <= 1
}
}
if candidate.dir != td {
for n := 1; n < len(queue); n++ {
if queue[n].dir == td {
if tf(queue[n].ts) {
temp := queue[n]
for m := n; m > 1; m-- {
queue[m] = queue[m - 1]
}
queue[1] = candidate
candidate = temp
}
break
}
}
}
queue = queue[1:]
} else {
queue = []Person{}
}
lt += 1
if candidate.ts > lt {
lt = candidate.ts
}
result[candidate.idx] = lt
ld = candidate.dir
}
return result
}
func main() {
result := getTimes(
[]int32{
0, 0, 1, 5,
//0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5,
//0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5,
//0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5,
//0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5, 0, 0, 1, 5,
},
[]int32{
0, 1, 1, 0,
//0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
//0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
//0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
//0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
},
)
fmt.Printf("result -> %v", result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment