[InterviewBit] Max Distance

Max Distance

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
int Solution::maximumGap(const vector<int> &A) {
vector<pair<int,int>> AA;
for(int i = 0; i < A.size(); i++) AA.push_back({A[i],i});
sort(begin(AA), end(AA));
int res = 0, ma = 0;
for(int i = AA.size() - 1; i >= 0; i--) {
auto [_, pos] = AA[i];
res = max(res, ma - pos);
ma = max(ma,pos);
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/21/PS/interviewbit/max-distance/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.