Skip to content

Instantly share code, notes, and snippets.

@BogdanIrimie
Created November 8, 2021 13:22
Show Gist options
  • Save BogdanIrimie/3d5dd2e2fa2b3b707d55c375966ba769 to your computer and use it in GitHub Desktop.
Save BogdanIrimie/3d5dd2e2fa2b3b707d55c375966ba769 to your computer and use it in GitHub Desktop.
Solution to the third task
import (
"sort"
)
func Solution(A []int, D int) int {
invertTimeWithStone := invertTimeWithStone(A)
for i := 0; i <= len(invertTimeWithStone); i++ {
var currentRevealedStones []int = make([]int, i)
copy(currentRevealedStones, invertTimeWithStone[:i])
maxDistance := maxDistanceJumped(currentRevealedStones, D)
if maxDistance + D >= len(A) {
return i;
}
}
return -1;
}
func invertTimeWithStone(A []int) []int{
timeStoneAppears := make([]int, len(A) + 1)
for i := 0; i < len(A); i++ {
if A[i] >= 0 {
timeStoneAppears[A[i]] = i
}
}
return timeStoneAppears
}
func maxDistanceJumped(A []int, maxJump int) int {
sort.Ints(A)
currentPosition := -1
var oldPosition int
for {
oldPosition = currentPosition
for time := len(A) - 1; time >=0 ; time-- {
if currentPosition + maxJump >= A[time] {
currentPosition = A[time]
break
}
}
if currentPosition == oldPosition {
break
}
}
return currentPosition
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment