Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created December 30, 2019 01:44
Show Gist options
  • Save luoheng23/ad339274c98739cbe5c49433c93f9f98 to your computer and use it in GitHub Desktop.
Save luoheng23/ad339274c98739cbe5c49433c93f9f98 to your computer and use it in GitHub Desktop.
func max(x, y int) int {
if x > y {
return x
}
return y
}
func replaceElements(arr []int) []int {
res := make([]int, len(arr))
res[len(res)-1] = -1
if len(res) >= 2 {
res[len(res)-2] = arr[len(arr)-1]
}
for i := len(res) - 3; i >= 0; i-- {
res[i] = max(arr[i+1], res[i+1])
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment