<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>

<p>Return <em><strong>the maximum value over all triplets of indices</strong></em> <code>(i, j, k)</code> <em>such that</em> <code>i &lt; j &lt; k</code><em>. </em>If all such triplets have a negative value, return <code>0</code>.</p>

<p>The <strong>value of a triplet of indices</strong> <code>(i, j, k)</code> is equal to <code>(nums[i] - nums[j]) * nums[k]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [12,6,1,2,7]
<strong>Output:</strong> 77
<strong>Explanation:</strong> 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. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,10,3,4,19]
<strong>Output:</strong> 133
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> 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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

from typing import List

class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        n = len(nums)
        
        # Edge case: If there are less than 3 elements, return 0
        if n < 3:
            return 0
        
        # Precompute the maximum prefix and suffix arrays
        max_prefix = [0] * n
        max_suffix = [0] * n
        
        # Fill the max_prefix array
        max_prefix[0] = nums[0]
        for i in range(1, n):
            max_prefix[i] = max(max_prefix[i - 1], nums[i])
        
        # Fill the max_suffix array
        max_suffix[n - 1] = nums[n - 1]
        for i in range(n - 2, -1, -1):
            max_suffix[i] = max(max_suffix[i + 1], nums[i])
        
        # Calculate the maximum triplet value
        max_value = 0
        for j in range(1, n - 1):
            # We need at least one element before and after j
            if max_prefix[j - 1] > nums[j] and max_suffix[j + 1] > 0:
                current_value = (max_prefix[j - 1] - nums[j]) * max_suffix[j + 1]
                max_value = max(max_value, current_value)
        
        return max_value

# Example usage:
# solution = Solution()
# print(solution.maximumTripletValue([12, 6, 1, 2, 7]))  # Output: 77
# print(solution.maximumTripletValue([1, 10, 3, 4, 19]))  # Output: 133
# print(solution.maximumTripletValue([1, 2, 3]))  # Output: 0
840