Skip to content

feat: add solutions to lc problems: No.2873,2874 #4261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<strong>输入:</strong>nums = [1,10,3,4,19]
<strong>输出:</strong>133
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
可以证明不存在值大于 133 的有序下标三元组。
可以证明不存在值大于 133 的有序下标三元组。
</pre>

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

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

我们可以用两个变量 $mx$ 和 $mx\_diff$ 分别维护前缀最大值和最大差值。遍历数组时,更新这两个变量,答案为所有 $mx\_diff \times nums[i]$ 的最大值。
我们用两个变量 $\textit{mx}$ 和 $\textit{mxDiff}$ 分别维护前缀最大值和最大差值,用一个变量 $\textit{ans}$ 维护答案。初始时,这些变量都为 $0$。

接下来,我们枚举数组的每个元素 $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)$。

枚举完所有元素后,返回答案 $\textit{ans}$。

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

Expand All @@ -81,10 +85,10 @@ tags:
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
ans = mx = mx_diff = 0
for num in nums:
ans = max(ans, mx_diff * num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx - num)
for x in nums:
ans = max(ans, mx_diff * x)
mx_diff = max(mx_diff, mx - x)
mx = max(mx, x)
return ans
```

Expand All @@ -93,14 +97,12 @@ class Solution:
```java
class Solution {
public long maximumTripletValue(int[] nums) {
long max, maxDiff, ans;
max = 0;
maxDiff = 0;
ans = 0;
for (int num : nums) {
ans = Math.max(ans, num * maxDiff);
max = Math.max(max, num);
maxDiff = Math.max(maxDiff, max - num);
long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
Expand All @@ -113,12 +115,12 @@ class Solution {
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long long ans = 0;
int mx = 0, mx_diff = 0;
for (int num : nums) {
ans = max(ans, 1LL * mx_diff * num);
mx = max(mx, num);
mx_diff = max(mx_diff, mx - num);
long long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = max(ans, mxDiff * x);
mxDiff = max(mxDiff, 1LL * mx - x);
mx = max(mx, x);
}
return ans;
}
Expand All @@ -129,11 +131,11 @@ public:

```go
func maximumTripletValue(nums []int) int64 {
ans, mx, mx_diff := 0, 0, 0
for _, num := range nums {
ans = max(ans, mx_diff*num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx-num)
ans, mx, mxDiff := 0, 0, 0
for _, x := range nums {
ans = max(ans, mxDiff*x)
mxDiff = max(mxDiff, mx-x)
mx = max(mx, x)
}
return int64(ans)
}
Expand All @@ -143,16 +145,36 @@ func maximumTripletValue(nums []int) int64 {

```ts
function maximumTripletValue(nums: number[]): number {
let [ans, mx, mx_diff] = [0, 0, 0];
for (const num of nums) {
ans = Math.max(ans, mx_diff * num);
mx = Math.max(mx, num);
mx_diff = Math.max(mx_diff, mx - num);
let [ans, mx, mxDiff] = [0, 0, 0];
for (const x of nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
let mut ans: i64 = 0;
let mut mx: i32 = 0;
let mut mx_diff: i32 = 0;

for &x in &nums {
ans = ans.max(mx_diff as i64 * x as i64);
mx_diff = mx_diff.max(mx - x);
mx = mx.max(x);
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<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.
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>
Expand Down Expand Up @@ -65,9 +65,13 @@ It can be shown that there are no ordered triplets of indices with a value great

<!-- solution:start -->

### Solution 1: Maintain Maximum Prefix Value and Maximum Difference
### Solution 1: Maintaining Prefix Maximum and Maximum Difference

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]$.
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$.

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)$.

After iterating through all elements, we return the answer $\textit{ans}$.

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

Expand All @@ -79,10 +83,10 @@ The time complexity is $O(n)$, where $n$ is the length of the array. The space c
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
ans = mx = mx_diff = 0
for num in nums:
ans = max(ans, mx_diff * num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx - num)
for x in nums:
ans = max(ans, mx_diff * x)
mx_diff = max(mx_diff, mx - x)
mx = max(mx, x)
return ans
```

Expand All @@ -91,14 +95,12 @@ class Solution:
```java
class Solution {
public long maximumTripletValue(int[] nums) {
long max, maxDiff, ans;
max = 0;
maxDiff = 0;
ans = 0;
for (int num : nums) {
ans = Math.max(ans, num * maxDiff);
max = Math.max(max, num);
maxDiff = Math.max(maxDiff, max - num);
long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
Expand All @@ -111,12 +113,12 @@ class Solution {
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long long ans = 0;
int mx = 0, mx_diff = 0;
for (int num : nums) {
ans = max(ans, 1LL * mx_diff * num);
mx = max(mx, num);
mx_diff = max(mx_diff, mx - num);
long long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = max(ans, mxDiff * x);
mxDiff = max(mxDiff, 1LL * mx - x);
mx = max(mx, x);
}
return ans;
}
Expand All @@ -127,11 +129,11 @@ public:

```go
func maximumTripletValue(nums []int) int64 {
ans, mx, mx_diff := 0, 0, 0
for _, num := range nums {
ans = max(ans, mx_diff*num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx-num)
ans, mx, mxDiff := 0, 0, 0
for _, x := range nums {
ans = max(ans, mxDiff*x)
mxDiff = max(mxDiff, mx-x)
mx = max(mx, x)
}
return int64(ans)
}
Expand All @@ -141,16 +143,36 @@ func maximumTripletValue(nums []int) int64 {

```ts
function maximumTripletValue(nums: number[]): number {
let [ans, mx, mx_diff] = [0, 0, 0];
for (const num of nums) {
ans = Math.max(ans, mx_diff * num);
mx = Math.max(mx, num);
mx_diff = Math.max(mx_diff, mx - num);
let [ans, mx, mxDiff] = [0, 0, 0];
for (const x of nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
let mut ans: i64 = 0;
let mut mx: i32 = 0;
let mut mx_diff: i32 = 0;

for &x in &nums {
ans = ans.max(mx_diff as i64 * x as i64);
mx_diff = mx_diff.max(mx - x);
mx = mx.max(x);
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long long ans = 0;
int mx = 0, mx_diff = 0;
for (int num : nums) {
ans = max(ans, 1LL * mx_diff * num);
mx = max(mx, num);
mx_diff = max(mx_diff, mx - num);
long long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = max(ans, mxDiff * x);
mxDiff = max(mxDiff, 1LL * mx - x);
mx = max(mx, x);
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
func maximumTripletValue(nums []int) int64 {
ans, mx, mx_diff := 0, 0, 0
for _, num := range nums {
ans = max(ans, mx_diff*num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx-num)
ans, mx, mxDiff := 0, 0, 0
for _, x := range nums {
ans = max(ans, mxDiff*x)
mxDiff = max(mxDiff, mx-x)
mx = max(mx, x)
}
return int64(ans)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Solution {
public long maximumTripletValue(int[] nums) {
long max, maxDiff, ans;
max = 0;
maxDiff = 0;
ans = 0;
for (int num : nums) {
ans = Math.max(ans, num * maxDiff);
max = Math.max(max, num);
maxDiff = Math.max(maxDiff, max - num);
long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
ans = mx = mx_diff = 0
for num in nums:
ans = max(ans, mx_diff * num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx - num)
for x in nums:
ans = max(ans, mx_diff * x)
mx_diff = max(mx_diff, mx - x)
mx = max(mx, x)
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
impl Solution {
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
let mut ans: i64 = 0;
let mut mx: i32 = 0;
let mut mx_diff: i32 = 0;

for &x in &nums {
ans = ans.max(mx_diff as i64 * x as i64);
mx_diff = mx_diff.max(mx - x);
mx = mx.max(x);
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function maximumTripletValue(nums: number[]): number {
let [ans, mx, mx_diff] = [0, 0, 0];
for (const num of nums) {
ans = Math.max(ans, mx_diff * num);
mx = Math.max(mx, num);
mx_diff = Math.max(mx_diff, mx - num);
let [ans, mx, mxDiff] = [0, 0, 0];
for (const x of nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
Loading