Leetcode 1231 Solution

This article provides solution to leetcode question 1231 (replace-elements-with-greatest-element-on-right-side)

https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side

Solution

class Solution:
    def replaceElements(self, arr: List[int]) -> List[int]:
        ans = [-1] * len(arr)

        i = len(arr) - 1
        curr = -1
        while i >= 0:
            ans[i] = curr
            curr = max(curr, arr[i])
            i -= 1

        return ans