-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1054. Distant Barcodes.java
41 lines (36 loc) · 1.26 KB
/
1054. Distant Barcodes.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
39
40
41
https://leetcode.com/problems/distant-barcodes/
solution: HashMap + maxHeap
First, use hash map to record the frequency of each barcode.
Then, construct the maxHeap, and the higher frequency has a higher priority.
Next, fill the res array according to priority. Fill the even index first, and
then odd index.
time complexity: O(n log n)
space complexity: O(n)
class Solution {
public int[] rearrangeBarcodes(int[] barcodes) {
int len = barcodes.length;
int[] res = new int[len];
HashMap<Integer, Integer> map = new HashMap<>();
for (int code : barcodes) {
map.put(code, map.getOrDefault(code, 0) + 1);
}
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[1] - a[1]);
for (int code : map.keySet()) {
int count = map.get(code);
pq.offer(new int[]{code, count});
}
int index = 0;
while (!pq.isEmpty()) {
int[] pair = pq.poll();
int code = pair[0], count = pair[1];
while (count-- > 0) {
res[index] = code;
index += 2;
if (index >= len) {
index = 1;
}
}
}
return res;
}
}