Skip to content

Commit 8c12bc8

Browse files
committed
Add solution #954
1 parent 7061201 commit 8c12bc8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-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,039 LeetCode solutions in JavaScript
1+
# 1,040 LeetCode solutions in JavaScript
22

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

@@ -763,6 +763,7 @@
763763
951|[Flip Equivalent Binary Trees](./solutions/0951-flip-equivalent-binary-trees.js)|Medium|
764764
952|[Largest Component Size by Common Factor](./solutions/0952-largest-component-size-by-common-factor.js)|Hard|
765765
953|[Verifying an Alien Dictionary](./solutions/0953-verifying-an-alien-dictionary.js)|Easy|
766+
954|[Array of Doubled Pairs](./solutions/0954-array-of-doubled-pairs.js)|Medium|
766767
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
767768
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
768769
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 954. Array of Doubled Pairs
3+
* https://leetcode.com/problems/array-of-doubled-pairs/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array of even length arr, return true if it is possible to reorder arr such
7+
* that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.
8+
*/
9+
10+
/**
11+
* @param {number[]} arr
12+
* @return {boolean}
13+
*/
14+
var canReorderDoubled = function(arr) {
15+
const map = new Map();
16+
arr.sort((a, b) => Math.abs(a) - Math.abs(b));
17+
18+
for (const num of arr) {
19+
map.set(num, (map.get(num) || 0) + 1);
20+
}
21+
for (const num of arr) {
22+
if (map.get(num) === 0) continue;
23+
if (!map.has(2 * num) || map.get(2 * num) === 0) return false;
24+
25+
map.set(num, map.get(num) - 1);
26+
map.set(2 * num, map.get(2 * num) - 1);
27+
}
28+
29+
return true;
30+
};

0 commit comments

Comments
 (0)