[LeetCode] Maximum Value of an Ordered Triplet I

2873. Maximum Value of an Ordered Triplet I

You are given a 0-indexed integer array nums.

Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.

The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long long ma = -1e9, best = 0, res = 0;
for(auto n : nums) {
res = max(res, best * n);
best = max(best, ma - n);
ma = max(ma, 1ll * n);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/10/01/PS/LeetCode/maximum-value-of-an-ordered-triplet-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.