1140. Distant Barcodes¶
Difficulty: Medium
LeetCode Problem View on GitHub
1140. Distant Barcodes
Medium
In a warehouse, there is a row of barcodes, where the ith barcode is barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
Example 1:
Input: barcodes = [1,1,1,2,2,2] Output: [2,1,2,1,2,1]
Example 2:
Input: barcodes = [1,1,1,1,2,2,3,3] Output: [1,3,1,3,1,2,1,2]
Constraints:
1 <= barcodes.length <= 100001 <= barcodes[i] <= 10000
Solution¶
class Solution {
static class Pair {
int node, freq, used;
public Pair(int node, int freq, int used) {
this.node = node;
this.freq = freq;
this.used = used;
}
@Override
public String toString() {
return "(" + node + " " + freq + " " + used + ")";
}
}
static class custom_sort implements Comparator<Pair> {
@Override
public int compare(Pair first, Pair second) {
return Integer.compare(second.freq, first.freq);
}
}
public int[] rearrangeBarcodes(int[] barcodes) {
int n = barcodes.length;
PriorityQueue<Pair> pq = new PriorityQueue<>(new custom_sort());
PriorityQueue<Pair> pq1 = new PriorityQueue<>(new custom_sort());
HashMap<Integer, Integer> map = new HashMap<>();
for (int ele : barcodes) map.put(ele, map.getOrDefault(ele, 0) + 1);
for (Map.Entry<Integer, Integer> curr : map.entrySet()) {
int key = curr.getKey();
int val = curr.getValue();
pq.offer(new Pair(key, val, 0));
}
int res[] = new int[n];
int k = 0;
while (pq.size() > 0) {
int key = pq.peek().node;
int freq = pq.peek().freq;
int used = pq.peek().used;
pq.poll();
res[k++] = key;
if (pq1.size() > 0) pq.offer(pq1.poll());
if (freq - 1 > 0) pq1.offer(new Pair(key, freq - 1, 1));
}
return res;
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here