↓Skip to main content
  1. LeetCode/

LeetCode 2874: Maximum Value of an Ordered Triplet II

·2 mins· ·
LeetCode Medium Prefix-Sum Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-31

  • Submissions: Wrong Answer → Output Limit Exceeded → Accepted

Approach
#

All numbers are positive, so with the middle j fixed, nums[i] and nums[k] should both be as large as possible (Prefix Sum Template 2):

  • i < j: the maximum of nums[0..j-1].
  • k > j: the maximum of nums[j+1..n-1].

A more space-efficient version scans once: at x, treat it as k and multiply by the best difference nums[i] - nums[j] seen so far; then treat it as j and update the best difference; finally treat it as i and update the maximum. This order guarantees i < j < k.

My Attempts
#

I used a prefix maximum array and a suffix maximum array built backward. Attempt 1 indexed the suffix array at n - j - 1, which is the maximum of nums[j..], counting j itself on the right. For [6, 2, 1] the only triplet gives (6 - 2) × 1 = 4; it used nums[j] = 2 as k and returned 8. After fixing it to n - j - 2, a leftover debug print caused Output Limit Exceeded; removing it passed.

Solution
#

class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        res = max_i = max_diff = 0
        for x in nums:
            res = max(res, max_diff * x)          # x as k
            max_diff = max(max_diff, max_i - x)   # x as j
            max_i = max(max_i, x)                 # x as i
        return res

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Index a backward-built suffix array carefully so it excludes j.
  • In the single pass, use before updating, in the order k, j, i.
  • Remove print before submitting.

Interview Explanation
#

All numbers are positive, so with the middle j fixed, both sides should be maximal. I scan once, tracking the running maximum and the best difference nums[i] - nums[j]; each number is first used as k, then updates the difference as j, then the maximum as i. Time O(n), space O(1).

Related

LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·2 mins
LeetCode Medium Array Matrix Prefix-Sum
LeetCode 3070 note: submatrices containing the top-left corner are exactly the 2D prefix sums P[i][j]; count those <= k
LeetCode 238: Product of Array Except Self
·2 mins
LeetCode Medium Array Prefix-Sum
LeetCode 238 note: each answer is the product of everything to the left times everything to the right; two passes, no division
LeetCode 2391: Minimum Amount of Time to Collect Garbage
·2 mins
LeetCode Medium Prefix-Sum String Array
LeetCode 2391 note: pickup time is the total garbage count; each truck only drives to the last house with its type, using a prefix sum of travel
LeetCode 3494: Find the Minimum Amount of Time to Brew Potions
·2 mins
LeetCode Medium Simulation Prefix-Sum Array
LeetCode 3494 note: for each potion, push forward to get the last wizard’s finish time (everyone must be free), then subtract backward to get each wizard’s finish time
LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them