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]From here.
Iterate from the back to the start,
We initilize mx = 1, where mx represent the max on the right.
Each round, we set A[i] = mx, where mx is its mas on the right.
Also we update mx = max(mx, A[i]), where A[i] is its original value.
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
A = arr
mx = -1
for i in range(len(A) - 1, -1, -1):
A[i], mx = mx, max(mx, A[i])
return ALast updated
Was this helpful?