1299. Replace Elements with Greatest Element on Right Side

🟩 Easy

Solution

My Solution

func replaceElements(arr []int) []int {
    num := -1

    for i:=len(arr)-1; i>=0; i-- {
        if arr[i] > num {
            num, arr[i] = arr[i], num
        } else {
            arr[i] = num
        }
    }

    return arr
}
result

Leetcode: link

Last updated

Was this helpful?