# 1299. Replace Elements with Greatest Element on Right Side ###### tags: `Leetcode` `Easy` Link: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/ ## 思路 $O(N)$ $O(1)$ ## Code ```java= class Solution { public int[] replaceElements(int[] arr) { int max = arr[arr.length-1]; arr[arr.length-1] = -1; for(int i = arr.length-2;i >= 0;i--){ int prevMax = max; max = Math.max(arr[i], max); arr[i] = prevMax; } return arr; } } ```