-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1982-find-array-given-subset-sums.js
More file actions
63 lines (56 loc) · 1.71 KB
/
1982-find-array-given-subset-sums.js
File metadata and controls
63 lines (56 loc) · 1.71 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
/**
* 1982. Find Array Given Subset Sums
* https://leetcode.com/problems/find-array-given-subset-sums/
* Difficulty: Hard
*
* You are given an integer n representing the length of an unknown array that you are trying
* to recover. You are also given an array sums containing the values of all 2n subset sums of
* the unknown array (in no particular order).
*
* Return the array ans of length n representing the unknown array. If multiple answers exist,
* return any of them.
*
* An array sub is a subset of an array arr if sub can be obtained from arr by deleting some
* (possibly zero or all) elements of arr. The sum of the elements in sub is one possible
* subset sum of arr. The sum of an empty array is considered to be 0.
*
* Note: Test cases are generated such that there will always be at least one correct answer.
*/
/**
* @param {number} n
* @param {number[]} sums
* @return {number[]}
*/
var recoverArray = function(n, sums) {
sums.sort((a, b) => a - b);
const result = [];
while (result.length < n) {
const diff = sums[1] - sums[0];
const withNum = [];
const withoutNum = [];
const freq = new Map();
for (const sum of sums) {
freq.set(sum, (freq.get(sum) || 0) + 1);
}
for (const sum of sums) {
if (freq.get(sum) > 0) {
freq.set(sum, freq.get(sum) - 1);
if (freq.get(sum + diff) > 0) {
freq.set(sum + diff, freq.get(sum + diff) - 1);
withoutNum.push(sum);
withNum.push(sum + diff);
} else {
return [];
}
}
}
if (withoutNum.includes(0)) {
result.push(diff);
sums = withoutNum;
} else {
result.push(-diff);
sums = withNum;
}
}
return result;
};