Skip to content

Commit e250870

Browse files
committed
Add solution and test-cases for problem 2740
1 parent 31756cd commit e250870

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [2740.Find the Value of the Partition][title]
2+
3+
## Description
4+
You are given a **positive** integer array `nums`.
5+
6+
Partition `nums` into two arrays, `nums1` and `nums2`, such that:
7+
8+
- Each element of the array `nums` belongs to either the array `nums1` or the array `nums2`.
9+
- Both arrays are **non-empty**.
10+
- The value of the partition is **minimized**.
11+
12+
The value of the partition is `|max(nums1) - min(nums2)|`.
13+
14+
Here, `max(nums1)` denotes the maximum element of the array `nums1`, and `min(nums2)` denotes the minimum element of the array `nums2`.
15+
16+
Return the integer denoting the value of such partition.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: nums = [1,3,2,4]
22+
Output: 1
23+
Explanation: We can partition the array nums into nums1 = [1,2] and nums2 = [3,4].
24+
- The maximum element of the array nums1 is equal to 2.
25+
- The minimum element of the array nums2 is equal to 3.
26+
The value of the partition is |2 - 3| = 1.
27+
It can be proven that 1 is the minimum value out of all partitions.
28+
Example
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: nums = [100,1,10]
35+
Output: 9
36+
Explanation: We can partition the array nums into nums1 = [10] and nums2 = [100,1].
37+
- The maximum element of the array nums1 is equal to 10.
38+
- The minimum element of the array nums2 is equal to 1.
39+
The value of the partition is |10 - 1| = 9.
40+
It can be proven that 9 is the minimum value out of all partitions.
41+
```
42+
43+
## 结语
44+
45+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
46+
47+
[title]: https://leetcode.com/problems/find-the-value-of-the-partition/
48+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) int {
6+
sort.Ints(nums)
7+
ans := -1
8+
for i := 1; i < len(nums); i++ {
9+
diff := nums[i] - nums[i-1]
10+
if ans == -1 || ans > diff {
11+
ans = diff
12+
}
13+
}
14+
return ans
515
}

leetcode/2701-2800/2740.Find-the-Value-of-the-Partition/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3, 4}, 1},
17+
{"TestCase2", []int{100, 1, 10}, 9},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)