https://leetcode.com/problems/distant-barcodes/
I have the following code which determines frequencies, and then inserts elements from barcode into PQ, and then staggers them back into barcodes[]
For test case [2,1,2,1,2,1,2,1,2,1....]
It works until the test case size becomes bigger than 255 elements
I don't really understand why that is. Can someone share some insight? Thanks.
class Solution {
public int[] rearrangeBarcodes(int[] barcodes) {
Map<Integer,Integer> numFreq = new HashMap<>();
for (int barcode : barcodes) {
int barFreq = numFreq.getOrDefault (barcode, 0);
numFreq.put (barcode, barFreq+1);
}
//System.out.println(numFreq);
PriorityQueue<Integer> pq = new PriorityQueue<> ((a,b) -> {
if (numFreq.get(b) == numFreq.get(a)) {
return a-b;
}
return numFreq.get(b) - numFreq.get(a);
});
for (int barcode : barcodes) {
pq.add (barcode);
}
for (int i = 0; i < barcodes.length; i+=2) {
barcodes[i] = pq.remove();
}
for (int i = 1; i < barcodes.length; i+=2) {
barcodes[i] = pq.remove();
}
return barcodes;
}
}