Skip to content

Commit 10b8226

Browse files
committed
Problem #2740
1 parent bb6d929 commit 10b8226

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using LeetCodeNet.Medium.Array;
2+
using System.Collections;
3+
4+
namespace LeetCodeNet.Tests.Medium.Array
5+
{
6+
public sealed class FindtheValueofthePartition_2740_test
7+
{
8+
[Theory, ClassData(typeof(FindtheValueofthePartitionTestData))]
9+
public void Check(int[] inputData, int expected)
10+
{
11+
var solver = new FindtheValueofthePartition_2740();
12+
13+
Assert.Equal(expected, solver.MinPartitionValue(inputData));
14+
}
15+
}
16+
17+
public sealed class FindtheValueofthePartitionTestData : IEnumerable<object[]>
18+
{
19+
public IEnumerator<object[]> GetEnumerator()
20+
{
21+
yield return new object[]
22+
{
23+
new int[] {1,2,3,4},
24+
1
25+
};
26+
27+
yield return new object[]
28+
{
29+
new int[] {100, 1, 10},
30+
9
31+
};
32+
33+
yield return new object[]
34+
{
35+
new int[] {-2, -1, 10, 20 , 30},
36+
1
37+
};
38+
}
39+
40+
IEnumerator IEnumerable.GetEnumerator()
41+
{
42+
return GetEnumerator();
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace LeetCodeNet.Medium.Array
2+
{
3+
/// <summary>
4+
/// https://leetcode.com/problems/find-the-value-of-the-partition/
5+
/// </summary>
6+
/// <remarks>
7+
/// You are given a positive integer array nums.
8+
/// Partition nums into two arrays, nums1 and nums2, such that:
9+
/// Each element of the array nums belongs to either the array nums1 or the array nums2.
10+
/// Both arrays are non-empty.
11+
/// The value of the partition is minimized.
12+
/// The value of the partition is |max(nums1) - min(nums2)|.
13+
/// Here, max(nums1) denotes the maximum element of the array nums1, and min(nums2) denotes the minimum element of the array nums2.
14+
/// Return the integer denoting the value of such partition.
15+
/// </remarks>
16+
internal sealed class FindtheValueofthePartition_2740
17+
{
18+
/// <summary>
19+
/// The partition will be minimized when the two consecutive elements in the source array have the minimum difference.
20+
/// This division will result in the first sub-array having the maximum value, and the following element will be the minimum of the next sub-array.
21+
/// To apply this approach, we need to sort the source array first and then successively check two elements to find the minimum difference.
22+
/// </summary>
23+
/// <param name="nums"> Input data </param>
24+
/// <returns> Min. number partition </returns>
25+
/// <remarks>
26+
/// Time complexity: O(n * log(n))
27+
/// Space complexity: O(1)
28+
/// </remarks>
29+
public int MinPartitionValue(int[] nums)
30+
{
31+
System.Array.Sort(nums);
32+
33+
var minValue = int.MaxValue;
34+
35+
for (var i = 1; i < nums.Length; i++)
36+
{
37+
minValue = Math.Min(Math.Abs(nums[i] - nums[i - 1]), minValue);
38+
}
39+
40+
return minValue;
41+
}
42+
}
43+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ All problems are grouped by patterns, so you can train in solving of specific ty
181181
* #347: Top K Frequent Elements (Array)
182182
* #692: Top K Frequent Words (Array, Bucket)
183183
* #2300: Successful Pairs of Spells and Potions (Array)
184+
* #2740: Find the Value of the Partition (Array)
184185
#### Pattern: Binary search
185186
* #33: Search in Rotated Sorted Array (Array)
186187
* #34: Find First and Last Position of Element in Sorted Array (Array)

0 commit comments

Comments
 (0)