-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
33 lines (24 loc) · 692 Bytes
/
main.cpp
File metadata and controls
33 lines (24 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
using namespace std;
int maximumGap(const vector<int> &num) {
if (num.size() == 0) return -1;
if (num.size() == 1) return 0;
vector<pair<int, int> > toSort;
for (int i = 0; i < num.size(); i++) {
toSort.push_back(make_pair(num[i], i));
}
sort(toSort.begin(), toSort.end());
int len = toSort.size();
int maxIndex = toSort[len - 1].second;
int ans = 0;
for (int i = len - 2; i >= 0; i--) {
ans = max(ans, maxIndex - toSort[i].second);
maxIndex = max(maxIndex, toSort[i].second);
}
return ans;
}
int main() {
vector<int> A = {3, 5, 4, 2};
cout << maximumGap(A);
}