File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,123 LeetCode solutions in JavaScript
1
+ # 1,124 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
859
859
1051|[ Height Checker] ( ./solutions/1051-height-checker.js ) |Easy|
860
860
1052|[ Grumpy Bookstore Owner] ( ./solutions/1052-grumpy-bookstore-owner.js ) |Medium|
861
861
1053|[ Previous Permutation With One Swap] ( ./solutions/1053-previous-permutation-with-one-swap.js ) |Medium|
862
+ 1054|[ Distant Barcodes] ( ./solutions/1054-distant-barcodes.js ) |Medium|
862
863
1071|[ Greatest Common Divisor of Strings] ( ./solutions/1071-greatest-common-divisor-of-strings.js ) |Easy|
863
864
1072|[ Flip Columns For Maximum Number of Equal Rows] ( ./solutions/1072-flip-columns-for-maximum-number-of-equal-rows.js ) |Medium|
864
865
1073|[ Adding Two Negabinary Numbers] ( ./solutions/1073-adding-two-negabinary-numbers.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1054. Distant Barcodes
3
+ * https://leetcode.com/problems/distant-barcodes/
4
+ * Difficulty: Medium
5
+ *
6
+ * In a warehouse, there is a row of barcodes, where the ith barcode is barcodes[i].
7
+ *
8
+ * Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer,
9
+ * and it is guaranteed an answer exists.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } barcodes
14
+ * @return {number[] }
15
+ */
16
+ var rearrangeBarcodes = function ( barcodes ) {
17
+ const frequencyMap = new Map ( ) ;
18
+ for ( const code of barcodes ) {
19
+ frequencyMap . set ( code , ( frequencyMap . get ( code ) || 0 ) + 1 ) ;
20
+ }
21
+
22
+ const sortedCodes = [ ...frequencyMap . entries ( ) ]
23
+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
24
+
25
+ const result = new Array ( barcodes . length ) ;
26
+ let index = 0 ;
27
+
28
+ for ( const [ code , count ] of sortedCodes ) {
29
+ for ( let i = 0 ; i < count ; i ++ ) {
30
+ if ( index >= barcodes . length ) index = 1 ;
31
+ result [ index ] = code ;
32
+ index += 2 ;
33
+ }
34
+ }
35
+
36
+ return result ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments