Skip to content

Commit 0bd8706

Browse files
authored
feature: add 2874 solution1 for C++
1 parent f6ef99d commit 0bd8706

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

solution/2874/sol1.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
long long maximumTripletValue(vector<int>& nums) {
4+
long long ans = 0;
5+
int n = nums.size();
6+
vector<int> prefix(n + 1, 0);
7+
for (int i = 0; i < n; ++i) {
8+
prefix[i + 1] = max(prefix[i], nums[i]);
9+
}
10+
int mx = nums[n - 1];
11+
for (int i = n - 2; i >= 1; --i) {
12+
ans = max(ans, 1LL * (prefix[i] - nums[i]) * mx);
13+
mx = max(mx, nums[i]);
14+
}
15+
return ans;
16+
}
17+
};

0 commit comments

Comments
 (0)