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

Commit 7693c48

Browse files
committed
Add solution #1982
1 parent 88c86e9 commit 7693c48

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,668 LeetCode solutions in JavaScript
1+
# 1,669 LeetCode solutions in JavaScript
22

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

@@ -1521,6 +1521,7 @@
15211521
1979|[Find Greatest Common Divisor of Array](./solutions/1979-find-greatest-common-divisor-of-array.js)|Easy|
15221522
1980|[Find Unique Binary String](./solutions/1980-find-unique-binary-string.js)|Medium|
15231523
1981|[Minimize the Difference Between Target and Chosen Elements](./solutions/1981-minimize-the-difference-between-target-and-chosen-elements.js)|Medium|
1524+
1982|[Find Array Given Subset Sums](./solutions/1982-find-array-given-subset-sums.js)|Hard|
15241525
1985|[Find the Kth Largest Integer in the Array](./solutions/1985-find-the-kth-largest-integer-in-the-array.js)|Medium|
15251526
1996|[The Number of Weak Characters in the Game](./solutions/1996-the-number-of-weak-characters-in-the-game.js)|Medium|
15261527
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* 1982. Find Array Given Subset Sums
3+
* https://leetcode.com/problems/find-array-given-subset-sums/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer n representing the length of an unknown array that you are trying
7+
* to recover. You are also given an array sums containing the values of all 2n subset sums of
8+
* the unknown array (in no particular order).
9+
*
10+
* Return the array ans of length n representing the unknown array. If multiple answers exist,
11+
* return any of them.
12+
*
13+
* An array sub is a subset of an array arr if sub can be obtained from arr by deleting some
14+
* (possibly zero or all) elements of arr. The sum of the elements in sub is one possible
15+
* subset sum of arr. The sum of an empty array is considered to be 0.
16+
*
17+
* Note: Test cases are generated such that there will always be at least one correct answer.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
* @param {number[]} sums
23+
* @return {number[]}
24+
*/
25+
var recoverArray = function(n, sums) {
26+
sums.sort((a, b) => a - b);
27+
28+
const result = [];
29+
while (result.length < n) {
30+
const diff = sums[1] - sums[0];
31+
32+
const withNum = [];
33+
const withoutNum = [];
34+
const freq = new Map();
35+
for (const sum of sums) {
36+
freq.set(sum, (freq.get(sum) || 0) + 1);
37+
}
38+
39+
for (const sum of sums) {
40+
if (freq.get(sum) > 0) {
41+
freq.set(sum, freq.get(sum) - 1);
42+
43+
if (freq.get(sum + diff) > 0) {
44+
freq.set(sum + diff, freq.get(sum + diff) - 1);
45+
withoutNum.push(sum);
46+
withNum.push(sum + diff);
47+
} else {
48+
return [];
49+
}
50+
}
51+
}
52+
53+
if (withoutNum.includes(0)) {
54+
result.push(diff);
55+
sums = withoutNum;
56+
} else {
57+
result.push(-diff);
58+
sums = withNum;
59+
}
60+
}
61+
62+
return result;
63+
};

0 commit comments

Comments
 (0)