From f06b00ea3cdcb1c5f8d75926d18f64d60439f4df Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 2 Apr 2025 09:29:16 +0800 Subject: [PATCH] Add solution and test-cases for problem 2873 --- .../README.md | 41 +++++++++++++++++++ .../Solution.go | 17 ++++++++ .../Solution_test.go | 39 ++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/README.md create mode 100644 leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution.go create mode 100644 leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution_test.go diff --git a/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/README.md b/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/README.md new file mode 100644 index 000000000..e27dbe607 --- /dev/null +++ b/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/README.md @@ -0,0 +1,41 @@ +# [2873.Maximum Value of an Ordered Triplet I][title] + +## Description +You are given a **0-indexed** integer array `nums`. + +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`. + +The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`. + +**Example 1:** + +``` +Input: nums = [12,6,1,2,7] +Output: 77 +Explanation: 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. +``` + +**Example 2:** + +``` +Input: nums = [1,10,3,4,19] +Output: 133 +Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133. +It can be shown that there are no ordered triplets of indices with a value greater than 133. +``` + +**Example 3:** + +``` +Input: nums = [1,2,3] +Output: 0 +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. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution.go b/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution.go new file mode 100644 index 000000000..e63298bf1 --- /dev/null +++ b/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution.go @@ -0,0 +1,17 @@ +package Solution + +func Solution(nums []int) int64 { + var ans int64 + cut := make([]int, len(nums)) + _max := nums[0] + for i := 1; i < len(nums); i++ { + cut[i] = _max - nums[i] + _max = max(_max, nums[i]) + } + _max = cut[1] + for i := 2; i < len(nums); i++ { + ans = max(ans, int64(nums[i])*int64(_max)) + _max = max(_max, cut[i]) + } + return ans +} diff --git a/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution_test.go b/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution_test.go new file mode 100644 index 000000000..96f1704d1 --- /dev/null +++ b/leetcode/2801-2900/2873.Maximum-Value-of-an-Ordered-Triplet-I/Solution_test.go @@ -0,0 +1,39 @@ +package Solution + +import ( + "reflect" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + inputs []int + expect int64 + }{ + {"TestCase1", []int{12, 6, 1, 2, 7}, 77}, + {"TestCase2", []int{1, 10, 3, 4, 19}, 133}, + {"TestCase3", []int{1, 2, 3}, 0}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.inputs) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v", + c.expect, got, c.inputs) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}