Skip to content

Latest commit

 

History

History

1299

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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]

 

Constraints:

  • 1 <= arr.length <= 10^4
  • 1 <= arr[i] <= 10^5

Related Topics:
Array

Solution 1.

// OJ: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    vector<int> replaceElements(vector<int>& A) {
        int mx = -1;
        for (int i = A.size() - 1; i >= 0; --i) {
            int n = A[i];
            A[i] = mx;
            mx = max(mx, n);
        }
        return A;
    }
};