Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f71ce91

Browse files
committed
Add solution #2035
1 parent b9f8785 commit f71ce91

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,699 LeetCode solutions in JavaScript
1+
# 1,700 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1560,6 +1560,7 @@
15601560
2030|[Smallest K-Length Subsequence With Occurrences of a Letter](./solutions/2030-smallest-k-length-subsequence-with-occurrences-of-a-letter.js)|Hard|
15611561
2032|[Two Out of Three](./solutions/2032-two-out-of-three.js)|Easy|
15621562
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
1563+
2035|[Partition Array Into Two Arrays to Minimize Sum Difference](./solutions/2035-partition-array-into-two-arrays-to-minimize-sum-difference.js)|Hard|
15631564
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
15641565
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
15651566
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* 2035. Partition Array Into Two Arrays to Minimize Sum Difference
3+
* https://leetcode.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums of 2 * n integers. You need to partition nums into two arrays
7+
* of length n to minimize the absolute difference of the sums of the arrays. To partition nums,
8+
* put each element of nums into one of the two arrays.
9+
*
10+
* Return the minimum possible absolute difference.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @return {number}
16+
*/
17+
var minimumDifference = function(nums) {
18+
const n = nums.length / 2;
19+
let result = Infinity;
20+
const totalSum = nums.reduce((sum, num) => sum + num, 0);
21+
const firstHalfSubsets = new Map();
22+
23+
for (let mask = 0; mask < (1 << n); mask++) {
24+
let size = 0;
25+
let subsetSum = 0;
26+
27+
for (let i = 0; i < n; i++) {
28+
if ((mask & (1 << i)) !== 0) {
29+
size++;
30+
subsetSum += nums[i];
31+
}
32+
}
33+
34+
if (!firstHalfSubsets.has(size)) {
35+
firstHalfSubsets.set(size, []);
36+
}
37+
firstHalfSubsets.get(size).push(subsetSum);
38+
}
39+
40+
for (const [size, sums] of firstHalfSubsets) {
41+
sums.sort((a, b) => a - b);
42+
}
43+
44+
for (let mask = 0; mask < (1 << n); mask++) {
45+
let size = 0;
46+
let secondHalfSum = 0;
47+
48+
for (let i = 0; i < n; i++) {
49+
if ((mask & (1 << i)) !== 0) {
50+
size++;
51+
secondHalfSum += nums[n + i];
52+
}
53+
}
54+
55+
const complementSize = n - size;
56+
const firstHalfSums = firstHalfSubsets.get(complementSize);
57+
const target = (totalSum - 2 * secondHalfSum) / 2;
58+
const closestIndex = binarySearch(firstHalfSums, target);
59+
60+
if (closestIndex < firstHalfSums.length) {
61+
result = Math.min(
62+
result, Math.abs(totalSum - 2 * (secondHalfSum + firstHalfSums[closestIndex]))
63+
);
64+
}
65+
66+
if (closestIndex > 0) {
67+
result = Math.min(
68+
result, Math.abs(totalSum - 2 * (secondHalfSum + firstHalfSums[closestIndex - 1]))
69+
);
70+
}
71+
}
72+
73+
return result;
74+
};
75+
76+
function binarySearch(arr, target) {
77+
let left = 0;
78+
let right = arr.length - 1;
79+
80+
if (right < 0) return 0;
81+
82+
while (left < right) {
83+
const mid = Math.floor((left + right) / 2);
84+
85+
if (arr[mid] < target) {
86+
left = mid + 1;
87+
} else {
88+
right = mid;
89+
}
90+
}
91+
92+
return left;
93+
}

0 commit comments

Comments
 (0)