Minimum Increment to Make Array Unique (LeetCode 945): Solution and Explanation
Each move adds 1 to one element of nums. Return the fewest moves that leave every value in the array distinct.
Intuition
Values can only go up. A number that collides with another has to climb to some value above it that nobody else uses.
Once the array is sorted, equal values sit together and every number only has to clear the numbers placed before it. The cheapest spot for it is the lowest value that is both free and at least its own value.
Keep that lowest free value in one variable, need. Each number lands on max(need, x), pays the gap, and need moves one past it. No number ever needs to look back.
Approach: Sort, then place on the lowest free value
- Sort
numsin increasing order. - Set
moves = 0andneed = 0. - For each
x, setneed = max(need, x), addneed - xtomoves, then increaseneedby 1. - Return
moves.
Step-by-step example
nums = [1,2,2]
- Sorted: [1,2,2]. need = 0, moves = 0.
- x = 1: need becomes max(0,1) = 1. Cost 0. need = 2.
- x = 2: need stays 2. Cost 0. need = 3.
- x = 2: value 2 is taken, need is 3. Cost 3 - 2 = 1, moves = 1. need = 4.
- Return 1.
Code: Python, Java, C++
1class Solution:
2 def minIncrementForUnique(self, nums: List[int]) -> int:
3 nums.sort()
4 moves = 0
5 need = 0
6 for x in nums:
7 need = max(need, x)
8 moves += need - x
9 need += 1
10 return moves1class Solution {
2 public int minIncrementForUnique(int[] nums) {
3 Arrays.sort(nums);
4 int moves = 0;
5 int need = 0;
6 for (int x : nums) {
7 need = Math.max(need, x);
8 moves += need - x;
9 need += 1;
10 }
11 return moves;
12 }
13}1class Solution {
2public:
3 int minIncrementForUnique(vector<int>& nums) {
4 sort(nums.begin(), nums.end());
5 int moves = 0;
6 int need = 0;
7 for (int x : nums) {
8 need = max(need, x);
9 moves += need - x;
10 need += 1;
11 }
12 return moves;
13 }
14};Time and space complexity
O(n log n)
Sorting dominates. The placement pass is a single O(n) loop.
O(1)
Two counters besides the in-place sort (which may use O(log n) stack depending on the language).
Other approaches
Counting buckets (time O(n + V) with V = max value, space O(V))
Values are at most 10^5, so count how many times each value appears. Walk the values upward carrying the surplus: at each value, keep one copy and push the rest up by one, adding the number pushed to the answer.
Union-find over free slots (time O(n alpha(n)), space O(V))
Treat each value as a slot whose parent points to the next free slot. Each number finds the free slot at or above it and links it to the following one. Works without sorting but is more code.
Common mistakes
- Only moving each duplicate to x + 1. That value may already be taken by a later number, so you must track the lowest free value across the whole pass.
- Forgetting need = max(need, x). When x is above every value used so far, the number keeps its own value and costs nothing.
- Counting distinct values and subtracting. The answer depends on how far each copy climbs, not just on how many copies there are.
- Using a set and incrementing one step at a time. That can be quadratic when many copies of one value pile up.
Frequently asked questions
Why is placing each number on the lowest free value optimal?
In sorted order, the lowest free value at or above x is the cheapest target for x, and taking it never makes a later number pay more, because every later number is at least x and would need a value above the ones already used anyway.
Does it matter which copy of a duplicate stays put?
No. Copies are identical, so whichever one keeps the value, the total climbing distance is the same.
Can the answer overflow?
The problem guarantees it fits in a 32-bit integer. A worst case of many equal values gives a sum like 0 + 1 + ... + (n - 1), which is why the guarantee is stated.