#2740
Find the Value of the Partition
MediumArraySorting
Approaches
💡
Intuition
Time Space
The brute force approach involves checking every possible way to partition the array into two non-empty subsets. We calculate the difference between the maximum of one subset and the minimum of the other for each partition.
⚙️
Algorithm
3 steps- 1Step 1: Iterate through all possible partitions of the array, ensuring both subsets are non-empty.
- 2Step 2: For each partition, calculate max(nums1) and min(nums2).
- 3Step 3: Compute the absolute difference |max(nums1) - min(nums2)| and keep track of the minimum difference found.
solution.py15 lines
1# Full working Python code
2
3def min_partition_value(nums):
4 n = len(nums)
5 min_value = float('inf')
6 for i in range(1, n):
7 nums1 = nums[:i]
8 nums2 = nums[i:]
9 max1 = max(nums1)
10 min2 = min(nums2)
11 min_value = min(min_value, abs(max1 - min2))
12 return min_value
13
14# Example usage
15print(min_partition_value([1, 3, 2, 4]))Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.