LetCode 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.

class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:
        n = len(arr)
        ans = [0] * (n-1) + [-1]
        for i in range(n-2, -1, -1):
            ans[i] = max(ans[i+1], arr[i+1])
        return ans
posted @ 2021-12-28 21:03  焰红火  阅读(73)  评论(0)    收藏  举报