|
| 1 | +# [2035. Partition Array Into Two Arrays to Minimize Sum Difference](https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +<div class="elfjS" data-track-load="description_content"><p>You are given an integer array <code>nums</code> of <code>2 * n</code> integers. You need to partition <code>nums</code> into <strong>two</strong> arrays of length <code>n</code> to <strong>minimize the absolute difference</strong> of the <strong>sums</strong> of the arrays. To partition <code>nums</code>, put each element of <code>nums</code> into <strong>one</strong> of the two arrays.</p> |
| 6 | + |
| 7 | +<p>Return <em>the <strong>minimum</strong> possible absolute difference</em>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | +<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/02/ex1.png" style="width: 240px; height: 106px;"> |
| 12 | +<pre><strong>Input:</strong> nums = [3,9,7,3] |
| 13 | +<strong>Output:</strong> 2 |
| 14 | +<strong>Explanation:</strong> One optimal partition is: [3,9] and [7,3]. |
| 15 | +The absolute difference between the sums of the arrays is abs((3 + 9) - (7 + 3)) = 2. |
| 16 | +</pre> |
| 17 | + |
| 18 | +<p><strong class="example">Example 2:</strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input:</strong> nums = [-36,36] |
| 21 | +<strong>Output:</strong> 72 |
| 22 | +<strong>Explanation:</strong> One optimal partition is: [-36] and [36]. |
| 23 | +The absolute difference between the sums of the arrays is abs((-36) - (36)) = 72. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 3:</strong></p> |
| 27 | +<img alt="example-3" src="https://assets.leetcode.com/uploads/2021/10/02/ex3.png" style="width: 316px; height: 106px;"> |
| 28 | +<pre><strong>Input:</strong> nums = [2,-1,0,4,-2,-9] |
| 29 | +<strong>Output:</strong> 0 |
| 30 | +<strong>Explanation:</strong> One optimal partition is: [2,4,-9] and [-1,0,-2]. |
| 31 | +The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p> </p> |
| 35 | +<p><strong>Constraints:</strong></p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li><code>1 <= n <= 15</code></li> |
| 39 | + <li><code>nums.length == 2 * n</code></li> |
| 40 | + <li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li> |
| 41 | +</ul> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +## Solutions |
| 47 | + |
| 48 | +**Solution: `Depth-First Search + Binary Search`** |
| 49 | + |
| 50 | +- Time complexity: <em>O(n\*2<sup>n</sup>)</em> |
| 51 | +- Space complexity: <em>O(2<sup>n</sup>)</em> |
| 52 | + |
| 53 | +<p> </p> |
| 54 | + |
| 55 | +### **JavaScript** |
| 56 | + |
| 57 | +```js |
| 58 | +/** |
| 59 | + * @param {number[]} nums |
| 60 | + * @return {number} |
| 61 | + */ |
| 62 | +const minimumDifference = function (nums) { |
| 63 | + const n = nums.length / 2; |
| 64 | + const sum = nums.reduce((result, num) => result + num); |
| 65 | + const goal = sum / 2; |
| 66 | + const leftNums = nums.slice(0, n); |
| 67 | + const rightNums = nums.slice(n); |
| 68 | + const leftSums = Array.from({ length: n + 1 }, () => []); |
| 69 | + const rightSums = Array.from({ length: n + 1 }, () => []); |
| 70 | + let result = Number.MAX_SAFE_INTEGER; |
| 71 | + |
| 72 | + const dfsSums = (nums, index, count, sum, sums) => { |
| 73 | + if (index >= n) { |
| 74 | + sums[count].push(sum); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const num = nums[index]; |
| 79 | + |
| 80 | + dfsSums(nums, index + 1, count, sum, sums); |
| 81 | + dfsSums(nums, index + 1, count + 1, sum + num, sums); |
| 82 | + }; |
| 83 | + |
| 84 | + dfsSums(leftNums, 0, 0, 0, leftSums); |
| 85 | + dfsSums(rightNums, 0, 0, 0, rightSums); |
| 86 | + |
| 87 | + const findFirstGreaterEqual = (nums, target) => { |
| 88 | + let left = 0; |
| 89 | + let right = nums.length - 1; |
| 90 | + |
| 91 | + while (left <= right) { |
| 92 | + const mid = Math.floor((left + right) / 2); |
| 93 | + |
| 94 | + nums[mid] >= target ? (right = mid - 1) : (left = mid + 1); |
| 95 | + } |
| 96 | + |
| 97 | + return left; |
| 98 | + }; |
| 99 | + |
| 100 | + for (let leftCount = 0; leftCount <= n; leftCount++) { |
| 101 | + const lSums = leftSums[leftCount]; |
| 102 | + const rSums = rightSums[n - leftCount]; |
| 103 | + |
| 104 | + rSums.sort((a, b) => a - b); |
| 105 | + |
| 106 | + for (const leftSum of lSums) { |
| 107 | + const index = findFirstGreaterEqual(rSums, goal - leftSum); |
| 108 | + |
| 109 | + if (index < rSums.length) { |
| 110 | + const sum1 = leftSum + rSums[index]; |
| 111 | + const sum2 = sum - sum1; |
| 112 | + |
| 113 | + result = Math.min(Math.abs(sum1 - sum2), result); |
| 114 | + } |
| 115 | + |
| 116 | + if (index > 0) { |
| 117 | + const sum1 = leftSum + rSums[index - 1]; |
| 118 | + const sum2 = sum - sum1; |
| 119 | + |
| 120 | + result = Math.min(Math.abs(sum1 - sum2), result); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return result; |
| 126 | +}; |
| 127 | +``` |
0 commit comments