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

Commit 5c5158d

Browse files
committed
Add solution #2007
1 parent cf7a7ca commit 5c5158d

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

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

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

@@ -1539,6 +1539,7 @@
15391539
2002|[Maximum Product of the Length of Two Palindromic Subsequences](./solutions/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js)|Medium|
15401540
2003|[Smallest Missing Genetic Value in Each Subtree](./solutions/2003-smallest-missing-genetic-value-in-each-subtree.js)|Hard|
15411541
2006|[Count Number of Pairs With Absolute Difference K](./solutions/2006-count-number-of-pairs-with-absolute-difference-k.js)|Easy|
1542+
2007|[Find Original Array From Doubled Array](./solutions/2007-find-original-array-from-doubled-array.js)|Medium|
15421543
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15431544
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15441545
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2007. Find Original Array From Doubled Array
3+
* https://leetcode.com/problems/find-original-array-from-doubled-array/
4+
* Difficulty: Medium
5+
*
6+
* An integer array original is transformed into a doubled array changed by appending twice the
7+
* value of every element in original, and then randomly shuffling the resulting array.
8+
*
9+
* Given an array changed, return original if changed is a doubled array. If changed is not a
10+
* doubled array, return an empty array. The elements in original may be returned in any order.
11+
*/
12+
13+
/**
14+
* @param {number[]} changed
15+
* @return {number[]}
16+
*/
17+
var findOriginalArray = function(changed) {
18+
if (changed.length % 2 !== 0) return [];
19+
20+
const frequency = new Map();
21+
const result = [];
22+
23+
changed.sort((a, b) => a - b);
24+
25+
for (const num of changed) {
26+
frequency.set(num, (frequency.get(num) || 0) + 1);
27+
}
28+
29+
for (const num of changed) {
30+
if (frequency.get(num) === 0) continue;
31+
32+
frequency.set(num, frequency.get(num) - 1);
33+
34+
const doubled = num * 2;
35+
if (!frequency.has(doubled) || frequency.get(doubled) === 0) return [];
36+
37+
frequency.set(doubled, frequency.get(doubled) - 1);
38+
result.push(num);
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)