package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/distant-barcodes/
 */
public class Problem1054 {
    public int[] rearrangeBarcodes(int[] barcodes) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1054 prob = new Problem1054();
        System.out.println(Arrays.toString(prob.rearrangeBarcodes(new int[]{1, 1, 1, 2, 2, 2}))); // [2,1,2,1,2,1]
        System.out.println(Arrays.toString(prob.rearrangeBarcodes(new int[]{1, 1, 1, 1, 2, 2, 3, 3}))); // [1,3,1,3,2,1,2,1]
    }
}

@@ -34,17 +34,123 @@
 il.Array
-s
+List;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.PriorityQueue
 ;%0A%0A/**%0A 
@@ -295,374 +295,1394 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1054 prob = new Problem1054();%0A        System.out.println(Arrays.toString(prob.rearrangeBarcodes(new int%5B%5D%7B1, 1, 1, 2, 2, 2%7D))); // %5B2,1,2,1,2,1%5D%0A        System.out.println(Arrays.toString(prob.rearrangeBarcodes(new int%5B%5D%7B1, 1, 1, 1, 2, 2, 3, 3%7D))); // %5B1,3,1,3,2,1,2,1%5D
+Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        for (int barcode : barcodes) %7B%0A            if (!map.containsKey(barcode)) %7B%0A                map.put(barcode, 1);%0A            %7D else %7B%0A                map.put(barcode, map.get(barcode) + 1);%0A            %7D%0A        %7D%0A        PriorityQueue%3CValueCount%3E queue = new PriorityQueue%3C%3E((a, b) -%3E Integer.compare(b.count, a.count));%0A        for (Map.Entry%3CInteger, Integer%3E e : map.entrySet()) %7B%0A            queue.add(new ValueCount(e.getKey(), e.getValue()));%0A        %7D%0A        int%5B%5D answer = new int%5Bbarcodes.length%5D;%0A        int i = 0;%0A        List%3CValueCount%3E tmp = new ArrayList%3C%3E();%0A        while (!queue.isEmpty()) %7B%0A            while (i - 1 %3E= 0 && answer%5Bi - 1%5D == queue.peek().value) %7B%0A                tmp.add(queue.remove());%0A            %7D%0A            ValueCount valueCount = queue.remove();%0A            answer%5Bi++%5D = valueCount.value;%0A            valueCount.count--;%0A            if (valueCount.count %3E 0) %7B%0A                queue.add(valueCount);%0A            %7D%0A            for (ValueCount vc : tmp) %7B%0A                queue.add(vc);%0A            %7D%0A            tmp.clear();%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static class ValueCount %7B%0A        private final int value;%0A        private int count;%0A%0A        public ValueCount(int value, int count) %7B%0A            this.value = value;%0A            this.count = count;%0A        %7D
 %0A   

Solve problem 1054