Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int minIncrementForUnique(int[] A) {
- Map<Integer, Integer> map = new HashMap<>();
- Set<Integer> visited = new HashSet<>();
- PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->(a-b));
- int i;
- for (i=0;i<A.length;i++)
- {
- map.put(A[i], map.getOrDefault(A[i],0)+1);
- visited.add(A[i]);
- }
- queue.addAll(visited);
- int count=0;
- while(!queue.isEmpty())
- {
- int key = queue.poll();
- int temp = key;
- int n = map.get(temp);
- if (n==1)
- continue;
- for (i=n-1;i>=1;i--)
- {
- count+=i;
- temp++;
- if (!visited.contains(temp))
- {
- visited.add(temp);
- queue.add(temp);
- }
- map.put(temp, map.getOrDefault(temp,0)+1);
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement