File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,039 LeetCode solutions in JavaScript
1
+ # 1,040 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
763
763
951|[ Flip Equivalent Binary Trees] ( ./solutions/0951-flip-equivalent-binary-trees.js ) |Medium|
764
764
952|[ Largest Component Size by Common Factor] ( ./solutions/0952-largest-component-size-by-common-factor.js ) |Hard|
765
765
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|
766
767
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
767
768
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
768
769
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments