Skip to content

Commit 01d3d7d

Browse files
authored
Merge pull request #1166 from 0xff-dev/2874
Add solution and test-cases for problem 2874
2 parents cd0dad4 + 051ae85 commit 01d3d7d

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [2874.Maximum Value of an Ordered Triplet II][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums`.
5+
6+
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`.
7+
8+
The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [12,6,1,2,7]
14+
Output: 77
15+
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
16+
It can be shown that there are no ordered triplets of indices with a value greater than 77.
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: nums = [1,10,3,4,19]
23+
Output: 133
24+
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
25+
It can be shown that there are no ordered triplets of indices with a value greater than 133.
26+
```
27+
28+
**Example 3:**
29+
30+
```
31+
Input: nums = [1,2,3]
32+
Output: 0
33+
Explanation: 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.
34+
```
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
39+
40+
[title]: https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii
41+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Solution
2+
3+
func Solution(nums []int) int64 {
4+
var ans int64
5+
cut := make([]int, len(nums))
6+
_max := nums[0]
7+
for i := 1; i < len(nums); i++ {
8+
cut[i] = _max - nums[i]
9+
_max = max(_max, nums[i])
10+
}
11+
_max = cut[1]
12+
for i := 2; i < len(nums); i++ {
13+
ans = max(ans, int64(nums[i])*int64(_max))
14+
_max = max(_max, cut[i])
15+
}
16+
return ans
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs []int
14+
expect int64
15+
}{
16+
{"TestCase1", []int{12, 6, 1, 2, 7}, 77},
17+
{"TestCase2", []int{1, 10, 3, 4, 19}, 133},
18+
{"TestCase3", []int{1, 2, 3}, 0},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := Solution(c.inputs)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27+
c.expect, got, c.inputs)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
}
36+
37+
// 使用案列
38+
func ExampleSolution() {
39+
}

0 commit comments

Comments
 (0)