package leetcode;

import java.util.Map;
import java.util.TreeMap;

/**
 * https://leetcode.com/problems/reduction-operations-to-make-the-array-elements-equal/
 */
public class Problem1887 {
    public int reductionOperations(int[] nums) {
        TreeMap<Integer, Integer> map = new TreeMap<>((a, b) -> Integer.compare(b, a));
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        int answer = 0;
        while (map.size() > 1) {
            Map.Entry<Integer, Integer> largestEntry = map.pollFirstEntry();
            Map.Entry<Integer, Integer> nextLargest = map.firstEntry();
            map.put(nextLargest.getKey(), nextLargest.getValue() + 1);
            int largestEntryCount = largestEntry.getValue() - 1;
            if (largestEntryCount > 0) {
                map.put(largestEntry.getKey(), largestEntryCount);
            }
            answer++;
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1887 prob = new Problem1887();
        System.out.println(prob.reductionOperations(new int[]{5,1,3})); // 3
        System.out.println(prob.reductionOperations(new int[]{1,1,1})); // 0
        System.out.println(prob.reductionOperations(new int[]{1,1,2,2,3})); // 4
    }
}

@@ -719,48 +719,8 @@
 () +
- 1);%0A            int largestEntryCount =
  lar
@@ -743,12 +743,9 @@
 ue()
- - 1
+)
 ;%0A  
@@ -758,505 +758,75 @@
     
-if (largestEntryCount %3E 0) %7B%0A                map.put(largestEntry.getKey(), largestEntryCount);%0A            %7D%0A            answer++;%0A        %7D%0A        return answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1887 prob = new Problem1887();%0A        System.out.println(prob.reductionOperations(new int%5B%5D%7B5,1,3%7D)); // 3%0A        System.out.println(prob.reductionOperations(new int%5B%5D%7B1,1,1%7D)); // 0%0A        System.out.println(prob.reductionOperations(new int%5B%5D%7B1,1,2,2,3%7D)); // 4
+answer += largestEntry.getValue();%0A        %7D%0A        return answer;
 %0A   

Solve problem 1887