Skip to content

Commit f857f2d

Browse files
committed
feat: add solution 2035. Partition Array Into Two Arrays to Minimize Sum Difference
1 parent 25fce5b commit f857f2d

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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>&nbsp;</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>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= n &lt;= 15</code></li>
39+
<li><code>nums.length == 2 * n</code></li>
40+
<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
41+
</ul>
42+
</div>
43+
44+
<p>&nbsp;</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>&nbsp;</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+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const minimumDifference = function (nums) {
6+
const n = nums.length / 2;
7+
const sum = nums.reduce((result, num) => result + num);
8+
const goal = sum / 2;
9+
const leftNums = nums.slice(0, n);
10+
const rightNums = nums.slice(n);
11+
const leftSums = Array.from({ length: n + 1 }, () => []);
12+
const rightSums = Array.from({ length: n + 1 }, () => []);
13+
let result = Number.MAX_SAFE_INTEGER;
14+
15+
const dfsSums = (nums, index, count, sum, sums) => {
16+
if (index >= n) {
17+
sums[count].push(sum);
18+
return;
19+
}
20+
21+
const num = nums[index];
22+
23+
dfsSums(nums, index + 1, count, sum, sums);
24+
dfsSums(nums, index + 1, count + 1, sum + num, sums);
25+
};
26+
27+
dfsSums(leftNums, 0, 0, 0, leftSums);
28+
dfsSums(rightNums, 0, 0, 0, rightSums);
29+
30+
const findFirstGreaterEqual = (nums, target) => {
31+
let left = 0;
32+
let right = nums.length - 1;
33+
34+
while (left <= right) {
35+
const mid = Math.floor((left + right) / 2);
36+
37+
nums[mid] >= target ? (right = mid - 1) : (left = mid + 1);
38+
}
39+
40+
return left;
41+
};
42+
43+
for (let leftCount = 0; leftCount <= n; leftCount++) {
44+
const lSums = leftSums[leftCount];
45+
const rSums = rightSums[n - leftCount];
46+
47+
rSums.sort((a, b) => a - b);
48+
49+
for (const leftSum of lSums) {
50+
const index = findFirstGreaterEqual(rSums, goal - leftSum);
51+
52+
if (index < rSums.length) {
53+
const sum1 = leftSum + rSums[index];
54+
const sum2 = sum - sum1;
55+
56+
result = Math.min(Math.abs(sum1 - sum2), result);
57+
}
58+
59+
if (index > 0) {
60+
const sum1 = leftSum + rSums[index - 1];
61+
const sum2 = sum - sum1;
62+
63+
result = Math.min(Math.abs(sum1 - sum2), result);
64+
}
65+
}
66+
}
67+
68+
return result;
69+
};

0 commit comments

Comments
 (0)