Skip to content

Commit b29a933

Browse files
committed
Runtime: 893 ms (Top 61.90%) | Memory: 51.3 MB (Top 87.91%)
1 parent 56144df commit b29a933

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

scripts/algorithms/P/Partition Array Into Two Arrays to Minimize Sum Difference/Partition Array Into Two Arrays to Minimize Sum Difference.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
// Runtime: 893 ms (Top 61.90%) | Memory: 51.3 MB (Top 87.91%)
12
class Solution {
23
public int minimumDifference(int[] nums) {
34
int n = nums.length;
45
int sum = 0;
56
for (int i : nums) {
67
sum += i;
78
}
8-
9+
910
TreeSet<Integer>[] sets = new TreeSet[n/2+1];
1011
for (int i = 0; i < (1 << (n / 2)); ++i) {
1112
int curSum = 0;
@@ -20,7 +21,7 @@ public int minimumDifference(int[] nums) {
2021
sets[m] = new TreeSet<Integer>();
2122
sets[m].add(curSum);
2223
}
23-
24+
2425
int res = Integer.MAX_VALUE / 3;
2526
for (int i = 0; i < (1 << (n / 2)); ++i) {
2627
int curSum = 0;
@@ -32,22 +33,22 @@ public int minimumDifference(int[] nums) {
3233
}
3334
}
3435
int target = (sum - 2 * curSum) / 2;
35-
36+
3637
Integer left = sets[n/2-m].floor(target), right = sets[n/2-m].ceiling(target);
3738
if (left != null) {
3839
res = Math.min(res, Math.abs(sum - 2 * (curSum + left.intValue())));
3940
}
40-
41+
4142
if (right != null) {
4243
res = Math.min(res, Math.abs(sum - 2 * (curSum + right.intValue())));
4344
}
44-
45+
4546
if (res == 0)
4647
return 0;
47-
48+
4849
}
49-
50+
5051
return res;
5152
}
5253
}
53-
// Time Complexity: O(2^(n/2) * n/2 * n/2)
54+
// Time Complexity: O(2^(n/2) * n/2 * n/2)

0 commit comments

Comments
 (0)