forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
51 lines (51 loc) · 1.48 KB
/
Solution.java
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
class Solution {
public int[] recoverArray(int n, int[] sums) {
int m = 1 << 30;
for (int x : sums) {
m = Math.min(m, x);
}
m = -m;
TreeMap<Integer, Integer> tm = new TreeMap<>();
for (int x : sums) {
tm.merge(x + m, 1, Integer::sum);
}
int[] ans = new int[n];
if (tm.merge(0, -1, Integer::sum) == 0) {
tm.remove(0);
}
ans[0] = tm.firstKey();
for (int i = 1; i < n; ++i) {
for (int j = 0; j < 1 << i; ++j) {
if ((j >> (i - 1) & 1) == 1) {
int s = 0;
for (int k = 0; k < i; ++k) {
if (((j >> k) & 1) == 1) {
s += ans[k];
}
}
if (tm.merge(s, -1, Integer::sum) == 0) {
tm.remove(s);
}
}
}
ans[i] = tm.firstKey();
}
for (int i = 0; i < 1 << n; ++i) {
int s = 0;
for (int j = 0; j < n; ++j) {
if (((i >> j) & 1) == 1) {
s += ans[j];
}
}
if (s == m) {
for (int j = 0; j < n; ++j) {
if (((i >> j) & 1) == 1) {
ans[j] *= -1;
}
}
break;
}
}
return ans;
}
}