-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2035-partition-array-into-two-arrays-to-minimize-sum-difference.js
More file actions
93 lines (77 loc) · 2.25 KB
/
2035-partition-array-into-two-arrays-to-minimize-sum-difference.js
File metadata and controls
93 lines (77 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* 2035. Partition Array Into Two Arrays to Minimize Sum Difference
* https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/
* Difficulty: Hard
*
* You are given an integer array nums of 2 * n integers. You need to partition nums into two arrays
* of length n to minimize the absolute difference of the sums of the arrays. To partition nums,
* put each element of nums into one of the two arrays.
*
* Return the minimum possible absolute difference.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var minimumDifference = function(nums) {
const n = nums.length / 2;
let result = Infinity;
const totalSum = nums.reduce((sum, num) => sum + num, 0);
const firstHalfSubsets = new Map();
for (let mask = 0; mask < (1 << n); mask++) {
let size = 0;
let subsetSum = 0;
for (let i = 0; i < n; i++) {
if ((mask & (1 << i)) !== 0) {
size++;
subsetSum += nums[i];
}
}
if (!firstHalfSubsets.has(size)) {
firstHalfSubsets.set(size, []);
}
firstHalfSubsets.get(size).push(subsetSum);
}
for (const [size, sums] of firstHalfSubsets) {
sums.sort((a, b) => a - b);
}
for (let mask = 0; mask < (1 << n); mask++) {
let size = 0;
let secondHalfSum = 0;
for (let i = 0; i < n; i++) {
if ((mask & (1 << i)) !== 0) {
size++;
secondHalfSum += nums[n + i];
}
}
const complementSize = n - size;
const firstHalfSums = firstHalfSubsets.get(complementSize);
const target = (totalSum - 2 * secondHalfSum) / 2;
const closestIndex = binarySearch(firstHalfSums, target);
if (closestIndex < firstHalfSums.length) {
result = Math.min(
result, Math.abs(totalSum - 2 * (secondHalfSum + firstHalfSums[closestIndex]))
);
}
if (closestIndex > 0) {
result = Math.min(
result, Math.abs(totalSum - 2 * (secondHalfSum + firstHalfSums[closestIndex - 1]))
);
}
}
return result;
};
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
if (right < 0) return 0;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}