Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created April 2, 2025 22:32
Show Gist options
  • Save Ifihan/a7906401665df4ad1f20083311fc955b to your computer and use it in GitHub Desktop.
Save Ifihan/a7906401665df4ad1f20083311fc955b to your computer and use it in GitHub Desktop.
Maximum Value of an Ordered Triplet I

Question

Approach

I iterated over all possible middle indices j and, for each, found the best i < j that gives the maximum nums[i]. Then, I scanned for all k > j and computed the triplet value. I kept track of the maximum of these values, and if none were positive, I returned 0.

Implementation

class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        n = len(nums)
        max_val = 0
        
        for j in range(1, n - 1):
            max_i = max(nums[:j])
            for k in range(j + 1, n):
                val = (max_i - nums[j]) * nums[k]
                max_val = max(max_val, val)
        
        return max_val

Complexities

  • Time: O(n^2)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment