https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
一番最後が -1 になるので、 変数 right_max に -1 を入れる。
後ろからループしていき、 現在の値 と right_max の大きい値を new_max に入れる。 現在の値に right_max を代入し、 right_max の値に nex_max を代入しループを続ける。
| 17 | 18 | 5 | 4 | 6 | 1 | -1
18 6 6 6 1 -1
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
right_max = -1
for i in range(len(arr) -1, -1, -1):
new_max = max(right_max, arr[i])
arr[i] = right_max
right_max = new_max