2874. Maximum Value of an Ordered Triplet II LeetCode Solution

In this guide, you will get 2874. Maximum Value of an Ordered Triplet II LeetCode Solution with the best time and space complexity. The solution to Maximum Value of an Ordered Triplet II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Value of an Ordered Triplet II solution in C++
  4. Maximum Value of an Ordered Triplet II solution in Java
  5. Maximum Value of an Ordered Triplet II solution in Python
  6. Additional Resources
2874. Maximum Value of an Ordered Triplet II LeetCode Solution image

Problem Statement of 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: 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.

See also  528. Random Pick with Weight LeetCode Solution

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 <= 105
1 <= nums[i] <= 106

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2874. Maximum Value of an Ordered Triplet II LeetCode Solution in C++

class Solution {
 public:
  // Same as 2873. Maximum Value of an Ordered Triplet I
  long long maximumTripletValue(vector<int>& nums) {
    long ans = 0;
    int maxDiff = 0;  // max(nums[i] - nums[j])
    int maxNum = 0;   // max(nums[i])

    for (const int num : nums) {
      ans = max(ans, static_cast<long>(maxDiff) * num);  // num := nums[k]
      maxDiff = max(maxDiff, maxNum - num);              // num := nums[j]
      maxNum = max(maxNum, num);                         // num := nums[i]
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2874. Maximum Value of an Ordered Triplet II LeetCode Solution in Java

class Solution {
  // Same as 2873. Maximum Value of an Ordered Triplet I
  public long maximumTripletValue(int[] nums) {
    long ans = 0;
    int maxDiff = 0; // max(nums[i] - nums[j])
    int maxNum = 0;  // max(nums[i])

    for (final int num : nums) {
      ans = Math.max(ans, (long) maxDiff * num); // num := nums[k]
      maxDiff = Math.max(maxDiff, maxNum - num); // num := nums[j]
      maxNum = Math.max(maxNum, num);            // num := nums[i]
    }

    return ans;
  }
}
// code provided by PROGIEZ

2874. Maximum Value of an Ordered Triplet II LeetCode Solution in Python

class Solution:
  # Same as 2873. Maximum Value of an Ordered Triplet I
  def maximumTripletValue(self, nums: list[int]) -> int:
    ans = 0
    maxDiff = 0  # max(nums[i] - nums[j])
    maxNum = 0   # max(nums[i])

    for num in nums:
      ans = max(ans, maxDiff * num)         # num := nums[k]
      maxDiff = max(maxDiff, maxNum - num)  # num := nums[j]
      maxNum = max(maxNum, num)             # num := nums[i]

    return ans
# code by PROGIEZ

Additional Resources

See also  1234. Replace the Substring for Balanced String LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.