3154. Maximum Value Of An Ordered Triplet I¶
Difficulty: Easy
LeetCode Problem View on GitHub
3154. Maximum Value of an Ordered Triplet I
Easy
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
Solution¶
class Solution {
public long maximumTripletValue(int[] nums) {
int n = nums.length;
long maxi = 0;
int pref_maxi[] = new int[n];
pref_maxi[n - 1] = nums[n - 1];
for (int i = n - 2; i >= 0; i--) pref_maxi[i] = Math.max(pref_maxi[i + 1], nums[i]);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (j + 1 < n) maxi = Math.max(maxi, (nums[i] - nums[j]) * 1L * pref_maxi[j + 1]);
}
}
return maxi;
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here