1. How I sloved
Because I had to find the biggest number after the designated element, I found the biggest number from the back. Then it was much easier because all I had to do was just compare the designated element with the very next. I started the comparing from ‘len(arr)-2’ and returned the second to last element of ‘arr’ and attached ‘-1’ to the end.
2. Code
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
for i in range(len(arr)-2,-1,-1):
arr[i] = max(arr[i],arr[i+1])
return arr[1:] + [-1]
# my first version, but took too much time
# class Solution:
# def replaceElements(self, arr: List[int]) -> List[int]:
# output = []
#
# for i in range(0,len(arr)-1):
# arr[i] = 0
# output.append(max(arr))
# output.append(-1)
#
# return output
3. Result
Runtime : 128 ms(82.97%), Memory usage : 14.9 MB(54.37%)
(Runtime can be different by a system even if it is a same code.)