Skip to content

Commit f984d56

Browse files
committed
Add solution #1054
1 parent 5befd97 commit f984d56

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

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

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

@@ -859,6 +859,7 @@
859859
1051|[Height Checker](./solutions/1051-height-checker.js)|Easy|
860860
1052|[Grumpy Bookstore Owner](./solutions/1052-grumpy-bookstore-owner.js)|Medium|
861861
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|
862863
1071|[Greatest Common Divisor of Strings](./solutions/1071-greatest-common-divisor-of-strings.js)|Easy|
863864
1072|[Flip Columns For Maximum Number of Equal Rows](./solutions/1072-flip-columns-for-maximum-number-of-equal-rows.js)|Medium|
864865
1073|[Adding Two Negabinary Numbers](./solutions/1073-adding-two-negabinary-numbers.js)|Medium|

solutions/1054-distant-barcodes.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
};

0 commit comments

Comments
 (0)