Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

The smaller input limit makes exhaustive search practical. Every valid choice is an index triple i < j < k, and its score is (nums[i] - nums[j]) * nums[k]; keeping the best directly avoids additional state or assumptions.

Approach

Use three nested loops with strictly increasing indices and evaluate the score of every triple. Track the largest score, then return it if positive or zero otherwise, as the problem requires for negative or zero maxima. The index restrictions are part of the definition, so sorting the values would change the problem and is not performed.

class Solution: def maximumTripletValue(self, nums: List[int]) -> int: n = len(nums) mx = float('-inf') for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): mx = max(mx, (nums[i] - nums[j]) * nums[k]) return mx if mx > 0 else 0

Complexity analysis

Let n = nums.length. Time: O(n^3). Auxiliary space: O(1).