You are 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 = [2,4,5,3,1,2]
Output: [5,5,3,2,2,-1]Example 2:
Input: arr = [3,3]
Output: [3,-1]Constraints:
1 <= arr.length <= 10,0001 <= arr[i] <= 100,000class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
n = len(arr)
ans = [0] * n
for i in range(n):
rightMax = -1
for j in range(i + 1, n):
rightMax = max(rightMax, arr[j])
ans[i] = rightMax
return ansclass Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
n = len(arr)
ans = [0] * n
rightMax = -1
for i in range(n - 1, -1, -1):
ans[i] = rightMax
rightMax = max(arr[i], rightMax)
return ans