1299. Replace Elements with Greatest Element on Right Side

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<int> replaceElements(vector<int>& arr) {
// 從右至左找最大
int n = arr.size();
vector<int> ret(n, -1);
// 17 18 5 4 6 1
// 18 6 6 6 1 -1
int mx = arr.back();
for(int i=n-2;i>-1;--i){
ret[i] = mx;
mx = max(mx, arr[i]);
}
return ret;
}
};

analysis

  • time complexity O(n)
  • space complexity O(n)