-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2874.cpp
More file actions
25 lines (24 loc) · 829 Bytes
/
2874.cpp
File metadata and controls
25 lines (24 loc) · 829 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
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long long res = 0;
long long maxVal = INT_MIN;
long long diff = 0;
int n = nums.size();
vector<long long> maxElementBackward(n, 0);
maxElementBackward[n - 1] = nums[n - 1];
for (int i = n - 2; i >= 0; --i) {
maxElementBackward[i] = max((long long)nums[i], maxElementBackward[i + 1]);
}
for (int i = 0; i < n; ++i) {
diff = max(diff, maxVal - nums[i]);
maxVal = max(maxVal, (long long)nums[i]);
if (i >= 1 && i < n - 1) {
long long right = maxElementBackward[i + 1];
long long curr = diff * right;
res = max(res, curr);
}
}
return res;
}
};