|
| 1 | +--- |
| 2 | +description: 'Author: @wkw | https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii' |
| 3 | +tags: [Array] |
| 4 | +--- |
| 5 | + |
| 6 | +# 2873 - Maximum Value of an Ordered Triplet II (Medium) |
| 7 | + |
| 8 | +## Problem Link |
| 9 | + |
| 10 | +https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii |
| 11 | + |
| 12 | +# Problem Statement |
| 13 | + |
| 14 | +You are given a 0-indexed integer array nums. |
| 15 | + |
| 16 | +Return the maximum value over all triplets of indices $(i, j, k)$ such that $i < j < k$. If all such triplets have a negative value, return $0$. |
| 17 | + |
| 18 | +The value of a triplet of indices $(i, j, k)$ is equal to $(nums[i] - nums[j]) * nums[k]$. |
| 19 | + |
| 20 | +## Examples |
| 21 | + |
| 22 | +### Example 1 |
| 23 | + |
| 24 | +``` |
| 25 | +Input: nums = [12,6,1,2,7] |
| 26 | +Output: 77 |
| 27 | +``` |
| 28 | + |
| 29 | +**Explanation:** |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +### Example 2 |
| 34 | + |
| 35 | +``` |
| 36 | +Input: nums = [1,10,3,4,19] |
| 37 | +Output: 133 |
| 38 | +``` |
| 39 | + |
| 40 | +**Explanation:** |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +### Example 3 |
| 45 | + |
| 46 | +``` |
| 47 | +Input: nums = [1,2,3] |
| 48 | +Output: 0 |
| 49 | +``` |
| 50 | + |
| 51 | +**Explanation:** |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +## Constraints |
| 56 | + |
| 57 | +- $3 <= nums.length <= 10^5$ |
| 58 | +- $1 <= nums[i] <= 10^6$ |
| 59 | + |
| 60 | +## Approach 1: Greedy |
| 61 | + |
| 62 | +When we are at an element $x$, we consider the candidate for different position. Let $i_{mx}$ be the maximum of $nums[i]$ and $d_{mx}$ be the maximum of the value of the diff $nums[i] - nums[j]$. At a point, we consider |
| 63 | + |
| 64 | +- $x$ as $nums[k]$, we can maximize the answer $res$ with $d_{mx} * x$. |
| 65 | +- $x$ as $nums[j]$, we can maximize the difference $d_{mx}$ with $i_{mx} - x$. |
| 66 | +- $x$ as $nums[i]$, we can maximize $i_{mx}$ with $i$. |
| 67 | + |
| 68 | +Time complexity is $O(n)$ |
| 69 | + |
| 70 | +Space comlexity is $O(1)$. |
| 71 | + |
| 72 | +<Tabs> |
| 73 | +<TabItem value="cpp" label="Python"> |
| 74 | +<SolutionAuthor name="@wkw"/> |
| 75 | + |
| 76 | +```py |
| 77 | +# O(n) |
| 78 | +class Solution: |
| 79 | + def maximumTripletValue(self, nums: List[int]) -> int: |
| 80 | + res, i_mx, d_mx = 0, 0, 0 |
| 81 | + for x in nums: |
| 82 | + res = max(res, d_mx * x) # x as nums[k] |
| 83 | + # maximize (nums[i] - nums[j]) |
| 84 | + d_mx = max(d_mx, i_mx - x) # x as nums[j] |
| 85 | + # maximize nums[i] |
| 86 | + i_mx = max(i_mx, x) # x as nums[i] |
| 87 | + return res |
| 88 | +``` |
| 89 | + |
| 90 | +</TabItem> |
| 91 | +</Tabs> |
0 commit comments