Leetcode Note: Go - Replace Elements With Greatest Element on Right Side
Replace Elements with Greatest Element on Right Side - LeetCode
https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
- Go 言語で取り組んだメモ
回答
O(n) easy 16ms Golang - Replace Elements with Greatest Element on Right Side - LeetCode
https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/solutions/1022072/o-n-easy-16ms-golang/
func replaceElements(arr []int) []int {
greatest := -1
for i := len(arr) - 1; i >= 0; i-- {
arr[i], greatest = greatest, max(greatest, arr[i])
}
return arr
}
func max(a, b int) int {
if a > b {
return a
}
return b
}