[LeetCode-Eazy] Replace Elements with Greatest Element on Right Side

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.

After doing so, return the array.

Example 1:

Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]

題目說明

最後一位數要變成 -1

每位數要變成右邊數來的最大數

func replaceElements(arr []int) []int {
  max := 0
  for i := len(arr) - 1; i >= 0; i-- {
    tmp := max
    if arr[i] > max {
      max = arr[i]
    }
    arr[i] = tmp
  }
  
  arr[len(arr) - 1] = -1
  return arr
}

我覺得這題難是難在我根本看不懂題目在說什麼 = =