Maximum Value of an Ordered Triplet I (LeetCode 2873): Solution and Explanation
For indices i < j < k, a triplet is worth (nums[i] - nums[j]) * nums[k]. Return the largest value over all triplets, or 0 if every triplet is negative. For [12,6,1,2,7] the answer is 77. Products reach 10^12, so the result is a 64-bit integer.
Intuition
All values are positive, so for a fixed k the product is largest when the gap nums[i] - nums[j] is as large as possible among indices before k.
For a fixed j, the best i is just the largest value to the left of j. So the best gap is a running maximum of max_i - nums[j].
Each element plays three roles as the scan passes it: first it is tried as k, then as j, then it may become the new i. Doing the updates in that order keeps the indices strictly increasing.
Approach: One pass with a prefix maximum and a best gap
- Set
ans = 0,max_i = 0andmax_diff = 0. - For each value
x, first updateans = max(ans, max_diff * x). Herexacts ask. - Then update
max_diff = max(max_diff, max_i - x). Herexacts asj. - Then update
max_i = max(max_i, x). Herexbecomes a candidatei. - Return
ans, computed with 64-bit arithmetic.
Step-by-step example
nums = [12,6,1,2,7]
- x = 12: no gap yet, so ans stays 0. It becomes max_i = 12.
- x = 6: as k it gives 0. As j the gap is 12 - 6 = 6, so max_diff = 6.
- x = 1: as k it gives 6 × 1 = 6, so ans = 6. As j the gap is 12 - 1 = 11, so max_diff = 11.
- x = 2: as k it gives 11 × 2 = 22, so ans = 22. The gap 12 - 2 = 10 is smaller than 11.
- x = 7: as k it gives 11 × 7 = 77, so ans = 77. The answer is 77 from the triplet (0, 2, 4).
Code: Python, Java, C++
1class Solution:
2 def maximumTripletValue(self, nums: List[int]) -> int:
3 ans = 0
4 max_i = 0
5 max_diff = 0
6 for x in nums:
7 ans = max(ans, max_diff * x)
8 max_diff = max(max_diff, max_i - x)
9 max_i = max(max_i, x)
10 return ans1class Solution {
2 public long maximumTripletValue(int[] nums) {
3 long ans = 0;
4 long maxI = 0;
5 long maxDiff = 0;
6 for (int x : nums) {
7 ans = Math.max(ans, maxDiff * x);
8 maxDiff = Math.max(maxDiff, maxI - x);
9 maxI = Math.max(maxI, x);
10 }
11 return ans;
12 }
13}1class Solution {
2public:
3 long long maximumTripletValue(vector<int>& nums) {
4 long long ans = 0;
5 long long maxI = 0;
6 long long maxDiff = 0;
7 for (int x : nums) {
8 ans = max(ans, maxDiff * x);
9 maxDiff = max(maxDiff, maxI - x);
10 maxI = max(maxI, (long long) x);
11 }
12 return ans;
13 }
14};Time and space complexity
O(n)
Each element is visited once and does three constant-time updates.
O(1)
Only three running values are kept.
Other approaches
Brute force over all triplets (time O(n^3), space O(1))
Three nested loops try every i < j < k. With n at most 100 this is about 160,000 triplets, which is accepted for this version of the problem.
Prefix and suffix maxima (time O(n), space O(n))
Precompute the largest value to the left of each j and the largest value to the right of it, then take (leftMax[j] - nums[j]) * rightMax[j] for every middle index.
Common mistakes
- Updating
max_ibeforemax_diff, ormax_diffbeforeans. Then the same element could act as two indices of one triplet. - Overflowing 32-bit integers. A gap near 10^6 times a value near 10^6 is about 10^12, so use
long,long longorint64. - Returning a negative number. Starting
ansat 0 handles the rule that a negative best becomes 0.
Frequently asked questions
Why is it safe to start max_i and max_diff at 0?
Every value is at least 1, so the first real value replaces max_i = 0. A max_diff of 0 produces a product of 0, which never beats the starting answer of 0.
Do I need the minimum value anywhere?
No. The multiplier nums[k] is always positive, so a negative gap can never help. Only the largest gap matters.
What changes in Maximum Value of an Ordered Triplet II?
Only the limits grow to 10^5 elements. The brute force stops working, but this one-pass solution is already linear and works unchanged.