package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/minimum-increment-to-make-array-unique/
 */
public class Problem945 {
    public int minIncrementForUnique(int[] A) {
        Arrays.sort(A);
        int answer = 0;
        for (int i = 0, j = 1; j < A.length; i++, j++) {
            while (A[j] <= A[i]) {
                A[j]++;
                answer++;
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem945 prob = new Problem945();
        System.out.println(prob.minIncrementForUnique(new int[]{1, 2, 2})); // 1
        System.out.println(prob.minIncrementForUnique(new int[]{3, 2, 1, 2, 1, 7})); // 6
    }
}

@@ -314,13 +314,10 @@
     
-while
+if
  (A%5B
@@ -350,55 +350,67 @@
     
-A%5Bj%5D++;%0A                answer++;%0A            %7D
+int count = A%5Bi%5D - A%5Bj%5D + 1;%0A                A%5Bj%5D += count;
 %0A   
@@ -418,18 +418,16 @@
     
-%7D%0A
         
 retu
@@ -426,289 +426,71 @@
     
-return answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem945 prob = new Problem945();%0A        System.out.println(prob.minIncrementForUnique(new int%5B%5D%7B1, 2, 2%7D)); // 1%0A        System.out.println(prob.minIncrementForUnique(new int%5B%5D%7B3, 2, 1, 2, 1, 7%7D)); // 6
+answer += count;%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 945