From e250870a0c9aeef9b04f3c84216f0bb27793453d Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 30 May 2025 09:28:15 +0800 Subject: [PATCH] Add solution and test-cases for problem 2740 --- .../README.md | 48 +++++++++++++++++++ .../Solution.go | 14 +++++- .../Solution_test.go | 13 +++-- 3 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/README.md diff --git a/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/README.md b/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/README.md new file mode 100644 index 000000000..c05857c4d --- /dev/null +++ b/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/README.md @@ -0,0 +1,48 @@ +# [2740.Find the Value of the Partition][title] + +## Description +You are given a **positive** integer array `nums`. + +Partition `nums` into two arrays, `nums1` and `nums2`, such that: + +- Each element of the array `nums` belongs to either the array `nums1` or the array `nums2`. +- Both arrays are **non-empty**. +- The value of the partition is **minimized**. + +The value of the partition is `|max(nums1) - min(nums2)|`. + +Here, `max(nums1)` denotes the maximum element of the array `nums1`, and `min(nums2)` denotes the minimum element of the array `nums2`. + +Return the integer denoting the value of such partition. + +**Example 1:** + +``` +Input: nums = [1,3,2,4] +Output: 1 +Explanation: We can partition the array nums into nums1 = [1,2] and nums2 = [3,4]. +- The maximum element of the array nums1 is equal to 2. +- The minimum element of the array nums2 is equal to 3. +The value of the partition is |2 - 3| = 1. +It can be proven that 1 is the minimum value out of all partitions. +Example +``` + +**Example 2:** + +``` +Input: nums = [100,1,10] +Output: 9 +Explanation: We can partition the array nums into nums1 = [10] and nums2 = [100,1]. +- The maximum element of the array nums1 is equal to 10. +- The minimum element of the array nums2 is equal to 1. +The value of the partition is |10 - 1| = 9. +It can be proven that 9 is the minimum value out of all partitions. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/find-the-value-of-the-partition/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution.go b/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution.go index d115ccf5e..ce3bb75f8 100755 --- a/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution.go +++ b/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution.go @@ -1,5 +1,15 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int) int { + sort.Ints(nums) + ans := -1 + for i := 1; i < len(nums); i++ { + diff := nums[i] - nums[i-1] + if ans == -1 || ans > diff { + ans = diff + } + } + return ans } diff --git a/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution_test.go b/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution_test.go index 14ff50eb4..294b41cef 100755 --- a/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution_test.go +++ b/leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3, 4}, 1}, + {"TestCase2", []int{100, 1, 10}, 9}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }