/
DistantBarcodes.java
38 lines (35 loc) · 1.12 KB
/
DistantBarcodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// https://leetcode.com/problems/distant-barcodes
class Solution {
public int[] rearrangeBarcodes(int[] arr) {
int[] freq = new int[10001];
int res[] = new int[arr.length], i = 0;
for (int x: arr) freq[x]++;
PriorityQueue<Pair<Integer, Integer>> queue = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
for (i = 1; i < freq.length; i++) {
if (freq[i] > 0)
queue.add(new Pair<>(i, freq[i]));
}
int last = -1;
i = 0;
Pair<Integer, Integer> temp = null;
while (!queue.isEmpty()) {
Pair<Integer, Integer> p = queue.poll();
int key = p.getKey();
if (key == last) {
temp = p;
} else {
res[i++] = key;
freq[key] -= 1;
if (freq[key] > 0) {
queue.add(new Pair<>(key, freq[key]));
}
if (temp != null) {
queue.add(temp);
temp = null;
}
last = key;
}
}
return res;
}
}