LeetCode: 1299. Replace Elements with Greatest Element on Right Side

less than 1 minute read

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]

My answer:

class Solution {
    public int[] replaceElements(int[] arr) {
        int ge = arr[arr.length - 1];
        arr[arr.length - 1] = -1;
        for(int i = arr.length - 2; i >= 0; i--){
            int temp = ge;
            if( arr[i] > ge) ge = arr[i];
            arr[i] = temp;
        }
        return arr;
    }
}

Perfect space save

class Solution {
    public int[] replaceElements(int[] arr) {
        for(int i = arr.length - 1,  mx = -1; i >= 0; i--){
            mx = Math.max(arr[i], arr[i] = mx);
        }
        return arr;
    }
}

Conclusion:

Using Math.max() method to save the space of temp variable. mx = Math.max(arr[i], arr[i] = mx);

Updated: