-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
executable file
·43 lines (35 loc) · 972 Bytes
/
solution.cpp
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
34
35
36
37
38
39
40
41
42
43
#include <vector>
#include <iostream>
using namespace std;
int maxSellingGap(const vector<int>& price) {
int num = price.size();
vector<int> suffix_max(num);
suffix_max[num - 1] = price[num - 1];
for (int idx = num - 2; idx >= 0; --idx) {
suffix_max[idx] = max(price[idx], suffix_max[idx + 1]);
}
int left = 0;
int right = 0;
int maxGap = 0;
int minPrefix = price[0];
while (left < num && right < num) {
int elem = price[left];
// 5 7 1 9 6 3 2
// 5 5 1 1 1 1 1 (minPrefix)
// 9 9 9 9 6 3 2 (suffix_max)
// ^
minPrefix = min(minPrefix, elem);
if (minPrefix <= suffix_max[right]) {
maxGap = max(maxGap, right - left);
++right;
} else {
++left;
}
}
return maxGap;
}
int main () {
vector<int> price = {5,7,1,9,6,3,2};
cout << maxSellingGap(price) << endl;
return 0;
}