1299. 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]

  • code
class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:
        right = arr[-1]
        arr[-1] = -1
        for i in range(len(arr)-2, -1, -1):
            cur = arr[i]
            arr[i] = right
            right = max(cur, right)
        return arr