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.
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
- Time: O(n^2)
- Space: O(1)
