Maximum Value of an Ordered Triplet I
EASYDescription
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: 77 Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77. It can be shown that there are no ordered triplets of indices with a value greater than 77.
Example 2:
Input: nums = [1,10,3,4,19] Output: 133 Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133. It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:
Input: nums = [1,2,3] Output: 0 Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
Constraints:
3 <= nums.length <= 1001 <= nums[i] <= 106
Approaches
Checkout 4 different approaches to solve Maximum Value of an Ordered Triplet I. Click on different approaches to view the approach and algorithm in detail.
Optimal Single Pass Linear Scan
The most optimal solution achieves both linear time and constant space complexity by processing the array in a single pass. This dynamic programming-style approach cleverly maintains the maximums needed at each step. As we iterate through the array, we consider each element as a potential nums[k], nums[j], and nums[i] in a specific order to build up the solution.
Algorithm
- Initialize
maxVal = 0,max_i = 0, andmax_ij = 0. max_iwill track the maximumnums[i]seen so far.max_ijwill track the maximumnums[i] - nums[j]seen so far.- Iterate through each
numin thenumsarray. - In each iteration, first update the result:
maxVal = max(maxVal, (long)max_ij * num). Here,numacts asnums[k]. - Then, update the maximum difference:
max_ij = max(max_ij, max_i - num). Here,numacts asnums[j]. - Finally, update the maximum prefix value:
max_i = max(max_i, num). Here,numacts asnums[i]. - Return
maxVal.
We iterate through the array while maintaining two key values:
max_i: The maximum value of an element seen so far (a potentialnums[i]).max_ij: The maximum differencenums[i] - nums[j]seen so far.
For each number num in the array, we perform three updates in a specific order:
- Treat
numasnums[k]: We calculate a potential answer by multiplyingnumwith the best(nums[i] - nums[j])difference (max_ij) found among the elements beforenum. We update our overallmaxValwith this result. - Treat
numasnums[j]: We updatemax_ij. A new, larger difference might be formed by usingnumasnums[j]and the largest element seen before it (max_i) asnums[i]. So we updatemax_ij = max(max_ij, max_i - num). - Treat
numasnums[i]: We updatemax_iby comparing it with the currentnum. This ensuresmax_iis ready for future elements to use as their potentialnums[j]ornums[k].
The strict order of these updates is crucial for the correctness of the algorithm.
class Solution {
public long maximumValue(int[] nums) {
long maxVal = 0;
int max_i = 0;
int max_ij = 0;
for (int num : nums) {
// Current num is nums[k]. max_ij is max(nums[i] - nums[j]) for i < j < k.
maxVal = Math.max(maxVal, (long)max_ij * num);
// Current num is nums[j]. max_i is max(nums[i]) for i < j.
max_ij = Math.max(max_ij, max_i - num);
// Current num is nums[i]. Update max_i for future iterations.
max_i = Math.max(max_i, num);
}
return maxVal;
}
}
Complexity Analysis
Pros and Cons
- Most efficient solution with O(n) time and O(1) space.
- Processes the array in a single pass.
- The logic can be less intuitive to grasp compared to more direct approaches.
Code Solutions
Checking out 3 solutions in different languages for Maximum Value of an Ordered Triplet I. Click on different languages to view the code.
class Solution {
public
long maximumTripletValue(int[] nums) {
long max, maxDiff, ans;
max = 0;
maxDiff = 0;
ans = 0;
for (int num : nums) {
ans = Math.max(ans, num * maxDiff);
max = Math.max(max, num);
maxDiff = Math.max(maxDiff, max - num);
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Maximum Value of an Ordered Triplet I
Similar Questions
5 related questions you might find useful
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.