File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
solution/1054.Distant Barcodes Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] rearrangeBarcodes (int [] barcodes ) {
3
+ Map <Integer , Integer > map = new HashMap <>();
4
+ for (int x : barcodes ) {
5
+ map .put (x , map .getOrDefault (x , 0 ) + 1 );
6
+ }
7
+ Data [] ds = new Data [map .size ()];
8
+ int i = 0 ;
9
+ for (Map .Entry <Integer , Integer > entry : map .entrySet ()) {
10
+ ds [i ++] = new Data (entry .getKey (), entry .getValue ());
11
+ }
12
+ Arrays .sort (ds );
13
+ i = 0 ;
14
+ for (Data d : ds ) {
15
+ while (d .cnt -- > 0 ) {
16
+ barcodes [i ] = d .x ;
17
+ i += 2 ;
18
+ if (i >= barcodes .length ) {
19
+ i = 1 ;
20
+ }
21
+ }
22
+ }
23
+ return barcodes ;
24
+ }
25
+
26
+ class Data implements Comparable <Data > {
27
+ int x , cnt ;
28
+
29
+ public Data (int x , int cnt ) {
30
+ this .x = x ;
31
+ this .cnt = cnt ;
32
+ }
33
+
34
+ @ Override
35
+ public int compareTo (Data o ) {
36
+ return Integer .compare (o .cnt , cnt );
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments