Mid/SeniorArray
Maximum Value of an Ordered Triplet II
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].
Example 1
Input
nums = [12,6,1,2,7]Output
77The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77, and there are no ordered triplets with a greater value.
Example 2
Input
nums = [1,10,3,4,19]Output
133The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133, and there are no ordered triplets with a greater value.
Constraints
- 3 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^6