Skip to content

Instantly share code, notes, and snippets.

@lorenzotinfena
Last active September 13, 2021 19:46
Show Gist options
  • Save lorenzotinfena/2898d74a6f52a311a697c0a4fc62e4da to your computer and use it in GitHub Desktop.
Save lorenzotinfena/2898d74a6f52a311a697c0a4fc62e4da to your computer and use it in GitHub Desktop.
my code of "Lord of Code | Italian Edition 2021" challenge
func slowestKey(keyTimes [][]int32) string {
a := [][]int32{{-1, 0}}
keyTimes = append(a, keyTimes...)
var max int32
var c int32
for i := 1; i < len(keyTimes); i++ {
if tmp := keyTimes[i][1] - keyTimes[i-1][1]; tmp > max{
max = tmp
c = keyTimes[i][0]
}
}
return string(c+97)
}
func minimumGroups(predators []int32) int32 {
pred := make([][]int, len(predators)+1)
for i, value := range predators {
pred[value+1] = append(pred[value+1], i+1)
}
return int32(find(pred, 0))-1
}
func find(pred[][]int, i int) int {
max := 0
for _, value := range pred[i] {
if tmp := find(pred, value); tmp > max {
max = tmp
}
}
return max+1
}
type node struct {
id int32
next, prev *node
}
func photoAlbum(index []int32, identity []int32) []int32 {
var head, tail *node
for i, pos := range index {
// find best starting node
if i == 0 {
head = &node{id: identity[i]}
tail = head
} else if j := int32(i); j - pos < pos {
// from tail
if pos == j {
//add to the end
tail.next = &node{id:identity[i], prev: tail}
tail = tail.next
} else {
for supp := tail;; supp = supp.prev {
j--
if j == pos {
prev := supp.prev
n := &node{id:identity[i], prev: prev, next: supp}
prev.next = n
supp.prev = n
break
}
}
}
} else {
// from head
if pos == 0 {
//add at begin
head.prev = &node{id: identity[i], next: head}
head = head.prev
} else {
j := int32(0)
for supp := head;; supp = supp.next {
j++
if j == pos {
next := supp.next
n := &node{id: identity[i], prev: supp, next: next}
supp.next = n
next.prev = n
break
}
}
}
}
}
list := make([]int32, len(index))
for i := 0; i < len(index); i++ {
list[i] = head.id
head = head.next
}
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment