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

浙公网安备 33010602011771号