public static long[] calculatePalette(long[] colors, int numBuckets) {
    ArrayList<Bucket> buckets = new ArrayList<>();
    Bucket initialBucket = new Bucket();
    for (long color : colors)
      initialBucket.add(color);
    buckets.add(initialBucket);

    while (buckets.size() < numBuckets) {
      int maxI = -1;
      int maxDiff = Integer.MIN_VALUE;
      for (int i = 0; i < buckets.size(); i++) {
        int diff = buckets.get(i).getMaxDiff();
        if (diff > maxDiff) {
          maxDiff = diff;
          maxI = i;
        }
      }
      if (maxDiff <= 0)
        break;
      buckets.add(buckets.get(maxI).split());
    }

    long[] palette = new long[buckets.size()];
    for (int i = 0; i < buckets.size(); i++)
      palette[i] = buckets.get(i).getAvgColor();
    return palette;
  }