← Home

1299. Replace Elements with Greatest Element on Right Side

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
129

The trick here is to name the state correctly, then let the implementation follow. For 1299. Replace Elements with Greatest Element on Right Side, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are replaceElements.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 52 ms, faster than 76.15% of C++ online submissions for Replace Elements with Greatest Element on Right Side.
02//Memory Usage: 10.7 MB, less than 100.00% of C++ online submissions for Replace Elements with Greatest Element on Right Side.
03
04class Solution {
05public:
06    vector<int> replaceElements(vector<int>& arr) {
07        int curMax = 0, tmp; //1 <= arr[i] <= 10^5
08        
09        for(int i = arr.size() - 1; i >= 0; i--){
10            tmp = arr[i];
11            if(i == arr.size() - 1){
12                arr[i] = -1;
13            }else{
14                arr[i] = curMax;
15            }
16            //prepared for next iteration
17            curMax = max(curMax, tmp);
18        }
19        
20        return arr;
21    }
22};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.