You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md
+53-31
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ tags:
31
31
<strong>Input:</strong> nums = [12,6,1,2,7]
32
32
<strong>Output:</strong> 77
33
33
<strong>Explanation:</strong> The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
34
-
It can be shown that there are no ordered triplets of indices with a value greater than 77.
34
+
It can be shown that there are no ordered triplets of indices with a value greater than 77.
35
35
</pre>
36
36
37
37
<p><strongclass="example">Example 2:</strong></p>
@@ -65,9 +65,13 @@ It can be shown that there are no ordered triplets of indices with a value great
65
65
66
66
<!-- solution:start -->
67
67
68
-
### Solution 1: Maintain Maximum Prefix Value and Maximum Difference
68
+
### Solution 1: Maintaining Prefix Maximum and Maximum Difference
69
69
70
-
We can use two variables $mx$ and $mx\_diff$ to maintain the maximum prefix value and maximum difference, respectively. When traversing the array, we update these two variables, and the answer is the maximum value of all $mx\_diff \times nums[i]$.
70
+
We use two variables $\textit{mx}$ and $\textit{mxDiff}$ to maintain the prefix maximum value and maximum difference, respectively, and use a variable $\textit{ans}$ to maintain the answer. Initially, these variables are all $0$.
71
+
72
+
Next, we iterate through each element $x$ in the array as $\textit{nums}[k]$. First, we update the answer $\textit{ans} = \max(\textit{ans}, \textit{mxDiff} \times x)$. Then we update the maximum difference $\textit{mxDiff} = \max(\textit{mxDiff}, \textit{mx} - x)$. Finally, we update the prefix maximum value $\textit{mx} = \max(\textit{mx}, x)$.
73
+
74
+
After iterating through all elements, we return the answer $\textit{ans}$.
71
75
72
76
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
73
77
@@ -79,10 +83,10 @@ The time complexity is $O(n)$, where $n$ is the length of the array. The space c
0 commit comments