Skip to content

Commit 1c7f7c0

Browse files
authored
feat: add solutions to lc problems: No.2873,2874 (#4261)
* No.2873.Maximum Value of an Ordered Triplet I * No.2874.Maximum Value of an Ordered Triplet II
1 parent ff62ef9 commit 1c7f7c0

File tree

16 files changed

+298
-184
lines changed

16 files changed

+298
-184
lines changed

solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README.md

+52-30
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<strong>输入:</strong>nums = [1,10,3,4,19]
4242
<strong>输出:</strong>133
4343
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
44-
可以证明不存在值大于 133 的有序下标三元组。
44+
可以证明不存在值大于 133 的有序下标三元组。
4545
</pre>
4646

4747
<p><strong class="example">示例 3:</strong></p>
@@ -69,7 +69,11 @@ tags:
6969

7070
### 方法一:维护前缀最大值和最大差值
7171

72-
我们可以用两个变量 $mx$ 和 $mx\_diff$ 分别维护前缀最大值和最大差值。遍历数组时,更新这两个变量,答案为所有 $mx\_diff \times nums[i]$ 的最大值。
72+
我们用两个变量 $\textit{mx}$ 和 $\textit{mxDiff}$ 分别维护前缀最大值和最大差值,用一个变量 $\textit{ans}$ 维护答案。初始时,这些变量都为 $0$。
73+
74+
接下来,我们枚举数组的每个元素 $x$ 作为 $\textit{nums}[k]$,首先更新答案 $\textit{ans} = \max(\textit{ans}, \textit{mxDiff} \times x)$,然后我们更新最大差值 $\textit{mxDiff} = \max(\textit{mxDiff}, \textit{mx} - x)$,最后更新前缀最大值 $\textit{mx} = \max(\textit{mx}, x)$。
75+
76+
枚举完所有元素后,返回答案 $\textit{ans}$。
7377

7478
时间复杂度 $O(n)$,其中 $n$ 是数组长度。空间复杂度 $O(1)$。
7579

@@ -81,10 +85,10 @@ tags:
8185
class Solution:
8286
def maximumTripletValue(self, nums: List[int]) -> int:
8387
ans = mx = mx_diff = 0
84-
for num in nums:
85-
ans = max(ans, mx_diff * num)
86-
mx = max(mx, num)
87-
mx_diff = max(mx_diff, mx - num)
88+
for x in nums:
89+
ans = max(ans, mx_diff * x)
90+
mx_diff = max(mx_diff, mx - x)
91+
mx = max(mx, x)
8892
return ans
8993
```
9094

@@ -93,14 +97,12 @@ class Solution:
9397
```java
9498
class Solution {
9599
public long maximumTripletValue(int[] nums) {
96-
long max, maxDiff, ans;
97-
max = 0;
98-
maxDiff = 0;
99-
ans = 0;
100-
for (int num : nums) {
101-
ans = Math.max(ans, num * maxDiff);
102-
max = Math.max(max, num);
103-
maxDiff = Math.max(maxDiff, max - num);
100+
long ans = 0, mxDiff = 0;
101+
int mx = 0;
102+
for (int x : nums) {
103+
ans = Math.max(ans, mxDiff * x);
104+
mxDiff = Math.max(mxDiff, mx - x);
105+
mx = Math.max(mx, x);
104106
}
105107
return ans;
106108
}
@@ -113,12 +115,12 @@ class Solution {
113115
class Solution {
114116
public:
115117
long long maximumTripletValue(vector<int>& nums) {
116-
long long ans = 0;
117-
int mx = 0, mx_diff = 0;
118-
for (int num : nums) {
119-
ans = max(ans, 1LL * mx_diff * num);
120-
mx = max(mx, num);
121-
mx_diff = max(mx_diff, mx - num);
118+
long long ans = 0, mxDiff = 0;
119+
int mx = 0;
120+
for (int x : nums) {
121+
ans = max(ans, mxDiff * x);
122+
mxDiff = max(mxDiff, 1LL * mx - x);
123+
mx = max(mx, x);
122124
}
123125
return ans;
124126
}
@@ -129,11 +131,11 @@ public:
129131
130132
```go
131133
func maximumTripletValue(nums []int) int64 {
132-
ans, mx, mx_diff := 0, 0, 0
133-
for _, num := range nums {
134-
ans = max(ans, mx_diff*num)
135-
mx = max(mx, num)
136-
mx_diff = max(mx_diff, mx-num)
134+
ans, mx, mxDiff := 0, 0, 0
135+
for _, x := range nums {
136+
ans = max(ans, mxDiff*x)
137+
mxDiff = max(mxDiff, mx-x)
138+
mx = max(mx, x)
137139
}
138140
return int64(ans)
139141
}
@@ -143,16 +145,36 @@ func maximumTripletValue(nums []int) int64 {
143145

144146
```ts
145147
function maximumTripletValue(nums: number[]): number {
146-
let [ans, mx, mx_diff] = [0, 0, 0];
147-
for (const num of nums) {
148-
ans = Math.max(ans, mx_diff * num);
149-
mx = Math.max(mx, num);
150-
mx_diff = Math.max(mx_diff, mx - num);
148+
let [ans, mx, mxDiff] = [0, 0, 0];
149+
for (const x of nums) {
150+
ans = Math.max(ans, mxDiff * x);
151+
mxDiff = Math.max(mxDiff, mx - x);
152+
mx = Math.max(mx, x);
151153
}
152154
return ans;
153155
}
154156
```
155157

158+
#### Rust
159+
160+
```rust
161+
impl Solution {
162+
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
163+
let mut ans: i64 = 0;
164+
let mut mx: i32 = 0;
165+
let mut mx_diff: i32 = 0;
166+
167+
for &x in &nums {
168+
ans = ans.max(mx_diff as i64 * x as i64);
169+
mx_diff = mx_diff.max(mx - x);
170+
mx = mx.max(x);
171+
}
172+
173+
ans
174+
}
175+
}
176+
```
177+
156178
<!-- tabs:end -->
157179

158180
<!-- solution:end -->

solution/2800-2899/2873.Maximum Value of an Ordered Triplet I/README_EN.md

+53-31
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131
<strong>Input:</strong> nums = [12,6,1,2,7]
3232
<strong>Output:</strong> 77
3333
<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.
3535
</pre>
3636

3737
<p><strong class="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
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1: Maintain Maximum Prefix Value and Maximum Difference
68+
### Solution 1: Maintaining Prefix Maximum and Maximum Difference
6969

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}$.
7175

7276
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
7377

@@ -79,10 +83,10 @@ The time complexity is $O(n)$, where $n$ is the length of the array. The space c
7983
class Solution:
8084
def maximumTripletValue(self, nums: List[int]) -> int:
8185
ans = mx = mx_diff = 0
82-
for num in nums:
83-
ans = max(ans, mx_diff * num)
84-
mx = max(mx, num)
85-
mx_diff = max(mx_diff, mx - num)
86+
for x in nums:
87+
ans = max(ans, mx_diff * x)
88+
mx_diff = max(mx_diff, mx - x)
89+
mx = max(mx, x)
8690
return ans
8791
```
8892

@@ -91,14 +95,12 @@ class Solution:
9195
```java
9296
class Solution {
9397
public long maximumTripletValue(int[] nums) {
94-
long max, maxDiff, ans;
95-
max = 0;
96-
maxDiff = 0;
97-
ans = 0;
98-
for (int num : nums) {
99-
ans = Math.max(ans, num * maxDiff);
100-
max = Math.max(max, num);
101-
maxDiff = Math.max(maxDiff, max - num);
98+
long ans = 0, mxDiff = 0;
99+
int mx = 0;
100+
for (int x : nums) {
101+
ans = Math.max(ans, mxDiff * x);
102+
mxDiff = Math.max(mxDiff, mx - x);
103+
mx = Math.max(mx, x);
102104
}
103105
return ans;
104106
}
@@ -111,12 +113,12 @@ class Solution {
111113
class Solution {
112114
public:
113115
long long maximumTripletValue(vector<int>& nums) {
114-
long long ans = 0;
115-
int mx = 0, mx_diff = 0;
116-
for (int num : nums) {
117-
ans = max(ans, 1LL * mx_diff * num);
118-
mx = max(mx, num);
119-
mx_diff = max(mx_diff, mx - num);
116+
long long ans = 0, mxDiff = 0;
117+
int mx = 0;
118+
for (int x : nums) {
119+
ans = max(ans, mxDiff * x);
120+
mxDiff = max(mxDiff, 1LL * mx - x);
121+
mx = max(mx, x);
120122
}
121123
return ans;
122124
}
@@ -127,11 +129,11 @@ public:
127129
128130
```go
129131
func maximumTripletValue(nums []int) int64 {
130-
ans, mx, mx_diff := 0, 0, 0
131-
for _, num := range nums {
132-
ans = max(ans, mx_diff*num)
133-
mx = max(mx, num)
134-
mx_diff = max(mx_diff, mx-num)
132+
ans, mx, mxDiff := 0, 0, 0
133+
for _, x := range nums {
134+
ans = max(ans, mxDiff*x)
135+
mxDiff = max(mxDiff, mx-x)
136+
mx = max(mx, x)
135137
}
136138
return int64(ans)
137139
}
@@ -141,16 +143,36 @@ func maximumTripletValue(nums []int) int64 {
141143

142144
```ts
143145
function maximumTripletValue(nums: number[]): number {
144-
let [ans, mx, mx_diff] = [0, 0, 0];
145-
for (const num of nums) {
146-
ans = Math.max(ans, mx_diff * num);
147-
mx = Math.max(mx, num);
148-
mx_diff = Math.max(mx_diff, mx - num);
146+
let [ans, mx, mxDiff] = [0, 0, 0];
147+
for (const x of nums) {
148+
ans = Math.max(ans, mxDiff * x);
149+
mxDiff = Math.max(mxDiff, mx - x);
150+
mx = Math.max(mx, x);
149151
}
150152
return ans;
151153
}
152154
```
153155

156+
#### Rust
157+
158+
```rust
159+
impl Solution {
160+
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
161+
let mut ans: i64 = 0;
162+
let mut mx: i32 = 0;
163+
let mut mx_diff: i32 = 0;
164+
165+
for &x in &nums {
166+
ans = ans.max(mx_diff as i64 * x as i64);
167+
mx_diff = mx_diff.max(mx - x);
168+
mx = mx.max(x);
169+
}
170+
171+
ans
172+
}
173+
}
174+
```
175+
154176
<!-- tabs:end -->
155177

156178
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
long long maximumTripletValue(vector<int>& nums) {
4-
long long ans = 0;
5-
int mx = 0, mx_diff = 0;
6-
for (int num : nums) {
7-
ans = max(ans, 1LL * mx_diff * num);
8-
mx = max(mx, num);
9-
mx_diff = max(mx_diff, mx - num);
4+
long long ans = 0, mxDiff = 0;
5+
int mx = 0;
6+
for (int x : nums) {
7+
ans = max(ans, mxDiff * x);
8+
mxDiff = max(mxDiff, 1LL * mx - x);
9+
mx = max(mx, x);
1010
}
1111
return ans;
1212
}
13-
};
13+
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
func maximumTripletValue(nums []int) int64 {
2-
ans, mx, mx_diff := 0, 0, 0
3-
for _, num := range nums {
4-
ans = max(ans, mx_diff*num)
5-
mx = max(mx, num)
6-
mx_diff = max(mx_diff, mx-num)
2+
ans, mx, mxDiff := 0, 0, 0
3+
for _, x := range nums {
4+
ans = max(ans, mxDiff*x)
5+
mxDiff = max(mxDiff, mx-x)
6+
mx = max(mx, x)
77
}
88
return int64(ans)
9-
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution {
22
public long maximumTripletValue(int[] nums) {
3-
long max, maxDiff, ans;
4-
max = 0;
5-
maxDiff = 0;
6-
ans = 0;
7-
for (int num : nums) {
8-
ans = Math.max(ans, num * maxDiff);
9-
max = Math.max(max, num);
10-
maxDiff = Math.max(maxDiff, max - num);
3+
long ans = 0, mxDiff = 0;
4+
int mx = 0;
5+
for (int x : nums) {
6+
ans = Math.max(ans, mxDiff * x);
7+
mxDiff = Math.max(mxDiff, mx - x);
8+
mx = Math.max(mx, x);
119
}
1210
return ans;
1311
}
14-
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def maximumTripletValue(self, nums: List[int]) -> int:
33
ans = mx = mx_diff = 0
4-
for num in nums:
5-
ans = max(ans, mx_diff * num)
6-
mx = max(mx, num)
7-
mx_diff = max(mx_diff, mx - num)
4+
for x in nums:
5+
ans = max(ans, mx_diff * x)
6+
mx_diff = max(mx_diff, mx - x)
7+
mx = max(mx, x)
88
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
3+
let mut ans: i64 = 0;
4+
let mut mx: i32 = 0;
5+
let mut mx_diff: i32 = 0;
6+
7+
for &x in &nums {
8+
ans = ans.max(mx_diff as i64 * x as i64);
9+
mx_diff = mx_diff.max(mx - x);
10+
mx = mx.max(x);
11+
}
12+
13+
ans
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function maximumTripletValue(nums: number[]): number {
2-
let [ans, mx, mx_diff] = [0, 0, 0];
3-
for (const num of nums) {
4-
ans = Math.max(ans, mx_diff * num);
5-
mx = Math.max(mx, num);
6-
mx_diff = Math.max(mx_diff, mx - num);
2+
let [ans, mx, mxDiff] = [0, 0, 0];
3+
for (const x of nums) {
4+
ans = Math.max(ans, mxDiff * x);
5+
mxDiff = Math.max(mxDiff, mx - x);
6+
mx = Math.max(mx, x);
77
}
88
return ans;
99
}

0 commit comments

Comments
 (0)