Skip to content

Commit 1e462ee

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

File tree

12 files changed

+316
-8
lines changed

12 files changed

+316
-8
lines changed

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

+56-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,27 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:维护前缀最大值和最大差值**
57+
58+
我们可以用两个变量 $mx$ 和 $mx\_diff$ 分别维护前缀最大值和最大差值。遍历数组时,更新这两个变量,答案为所有 $mx\_diff \times nums[i]$ 的最大值。
59+
60+
时间复杂度 $O(n)$,其中 $n$ 是数组长度。空间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
<!-- 这里可写当前语言的特殊实现逻辑 -->
6167

6268
```python
63-
69+
class Solution:
70+
def maximumTripletValue(self, nums: List[int]) -> int:
71+
ans = mx = mx_diff = 0
72+
for num in nums:
73+
ans = max(ans, mx_diff * num)
74+
mx = max(mx, num)
75+
mx_diff = max(mx_diff, mx - num)
76+
return ans
6477
```
6578

6679
### **Java**
@@ -88,13 +101,54 @@ class Solution {
88101
### **C++**
89102

90103
```cpp
91-
104+
class Solution {
105+
public:
106+
long long maximumTripletValue(vector<int>& nums) {
107+
long long ans = 0;
108+
int mx = 0, mx_diff = 0;
109+
for (int num : nums) {
110+
ans = max(ans, 1LL * mx_diff * num);
111+
mx = max(mx, num);
112+
mx_diff = max(mx_diff, mx - num);
113+
}
114+
return ans;
115+
}
116+
};
92117
```
93118
94119
### **Go**
95120
96121
```go
122+
func maximumTripletValue(nums []int) int64 {
123+
ans, mx, mx_diff := 0, 0, 0
124+
for _, num := range nums {
125+
ans = max(ans, mx_diff*num)
126+
mx = max(mx, num)
127+
mx_diff = max(mx_diff, mx-num)
128+
}
129+
return int64(ans)
130+
}
131+
132+
func max(a, b int) int {
133+
if a > b {
134+
return a
135+
}
136+
return b
137+
}
138+
```
139+
140+
### **TypeScript**
97141

142+
```ts
143+
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);
149+
}
150+
return ans;
151+
}
98152
```
99153

100154
### **...**

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

+56-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,25 @@ It can be shown that there are no ordered triplets of indices with a value great
4747

4848
## Solutions
4949

50+
**Solution 1: Maintain Maximum Prefix Value and Maximum Difference**
51+
52+
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]$.
53+
54+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
```python
55-
61+
class Solution:
62+
def maximumTripletValue(self, nums: List[int]) -> int:
63+
ans = mx = mx_diff = 0
64+
for num in nums:
65+
ans = max(ans, mx_diff * num)
66+
mx = max(mx, num)
67+
mx_diff = max(mx_diff, mx - num)
68+
return ans
5669
```
5770

5871
### **Java**
@@ -78,13 +91,54 @@ class Solution {
7891
### **C++**
7992

8093
```cpp
81-
94+
class Solution {
95+
public:
96+
long long maximumTripletValue(vector<int>& nums) {
97+
long long ans = 0;
98+
int mx = 0, mx_diff = 0;
99+
for (int num : nums) {
100+
ans = max(ans, 1LL * mx_diff * num);
101+
mx = max(mx, num);
102+
mx_diff = max(mx_diff, mx - num);
103+
}
104+
return ans;
105+
}
106+
};
82107
```
83108
84109
### **Go**
85110
86111
```go
112+
func maximumTripletValue(nums []int) int64 {
113+
ans, mx, mx_diff := 0, 0, 0
114+
for _, num := range nums {
115+
ans = max(ans, mx_diff*num)
116+
mx = max(mx, num)
117+
mx_diff = max(mx_diff, mx-num)
118+
}
119+
return int64(ans)
120+
}
121+
122+
func max(a, b int) int {
123+
if a > b {
124+
return a
125+
}
126+
return b
127+
}
128+
```
129+
130+
### **TypeScript**
87131

132+
```ts
133+
function maximumTripletValue(nums: number[]): number {
134+
let [ans, mx, mx_diff] = [0, 0, 0];
135+
for (const num of nums) {
136+
ans = Math.max(ans, mx_diff * num);
137+
mx = Math.max(mx, num);
138+
mx_diff = Math.max(mx_diff, mx - num);
139+
}
140+
return ans;
141+
}
88142
```
89143

90144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
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);
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
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)
7+
}
8+
return int64(ans)
9+
}
10+
11+
func max(a, b int) int {
12+
if a > b {
13+
return a
14+
}
15+
return b
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maximumTripletValue(self, nums: List[int]) -> int:
3+
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)
8+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
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);
7+
}
8+
return ans;
9+
}

solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,27 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:维护前缀最大值和最大差值**
57+
58+
我们可以用两个变量 $mx$ 和 $mx\_diff$ 分别维护前缀最大值和最大差值。遍历数组时,更新这两个变量,答案为所有 $mx\_diff \times nums[i]$ 的最大值。
59+
60+
时间复杂度 $O(n)$,其中 $n$ 是数组长度。空间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
<!-- 这里可写当前语言的特殊实现逻辑 -->
6167

6268
```python
63-
69+
class Solution:
70+
def maximumTripletValue(self, nums: List[int]) -> int:
71+
ans = mx = mx_diff = 0
72+
for num in nums:
73+
ans = max(ans, mx_diff * num)
74+
mx = max(mx, num)
75+
mx_diff = max(mx_diff, mx - num)
76+
return ans
6477
```
6578

6679
### **Java**
@@ -88,13 +101,54 @@ class Solution {
88101
### **C++**
89102

90103
```cpp
91-
104+
class Solution {
105+
public:
106+
long long maximumTripletValue(vector<int>& nums) {
107+
long long ans = 0;
108+
int mx = 0, mx_diff = 0;
109+
for (int num : nums) {
110+
ans = max(ans, 1LL * mx_diff * num);
111+
mx = max(mx, num);
112+
mx_diff = max(mx_diff, mx - num);
113+
}
114+
return ans;
115+
}
116+
};
92117
```
93118
94119
### **Go**
95120
96121
```go
122+
func maximumTripletValue(nums []int) int64 {
123+
ans, mx, mx_diff := 0, 0, 0
124+
for _, num := range nums {
125+
ans = max(ans, mx_diff*num)
126+
mx = max(mx, num)
127+
mx_diff = max(mx_diff, mx-num)
128+
}
129+
return int64(ans)
130+
}
131+
132+
func max(a, b int) int {
133+
if a > b {
134+
return a
135+
}
136+
return b
137+
}
138+
```
139+
140+
### **TypeScript**
97141

142+
```ts
143+
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);
149+
}
150+
return ans;
151+
}
98152
```
99153

100154
### **...**

solution/2800-2899/2874.Maximum Value of an Ordered Triplet II/README_EN.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,25 @@ It can be shown that there are no ordered triplets of indices with a value great
4747

4848
## Solutions
4949

50+
**Solution 1: Maintain Maximum Prefix Value and Maximum Difference**
51+
52+
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]$.
53+
54+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
```python
55-
61+
class Solution:
62+
def maximumTripletValue(self, nums: List[int]) -> int:
63+
ans = mx = mx_diff = 0
64+
for num in nums:
65+
ans = max(ans, mx_diff * num)
66+
mx = max(mx, num)
67+
mx_diff = max(mx_diff, mx - num)
68+
return ans
5669
```
5770

5871
### **Java**
@@ -78,13 +91,54 @@ class Solution {
7891
### **C++**
7992

8093
```cpp
81-
94+
class Solution {
95+
public:
96+
long long maximumTripletValue(vector<int>& nums) {
97+
long long ans = 0;
98+
int mx = 0, mx_diff = 0;
99+
for (int num : nums) {
100+
ans = max(ans, 1LL * mx_diff * num);
101+
mx = max(mx, num);
102+
mx_diff = max(mx_diff, mx - num);
103+
}
104+
return ans;
105+
}
106+
};
82107
```
83108
84109
### **Go**
85110
86111
```go
112+
func maximumTripletValue(nums []int) int64 {
113+
ans, mx, mx_diff := 0, 0, 0
114+
for _, num := range nums {
115+
ans = max(ans, mx_diff*num)
116+
mx = max(mx, num)
117+
mx_diff = max(mx_diff, mx-num)
118+
}
119+
return int64(ans)
120+
}
121+
122+
func max(a, b int) int {
123+
if a > b {
124+
return a
125+
}
126+
return b
127+
}
128+
```
129+
130+
### **TypeScript**
87131

132+
```ts
133+
function maximumTripletValue(nums: number[]): number {
134+
let [ans, mx, mx_diff] = [0, 0, 0];
135+
for (const num of nums) {
136+
ans = Math.max(ans, mx_diff * num);
137+
mx = Math.max(mx, num);
138+
mx_diff = Math.max(mx_diff, mx - num);
139+
}
140+
return ans;
141+
}
88142
```
89143

90144
### **...**

0 commit comments

Comments
 (0)