Skip to content

Commit f079e01

Browse files
authored
Create Solution.java
1 parent aaf587c commit f079e01

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)