Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.
Return the minimum number of moves to make every value in nums unique.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: nums = [1,2,2] Output: 1 Explanation: After 1 move, the array could be [1, 2, 3].
Example 2:
Input: nums = [3,2,1,2,1,7] Output: 6 Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105Problem summary: You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1. Return the minimum number of moves to make every value in nums unique. The test cases are generated so that the answer fits in a 32-bit integer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,2]
[3,2,1,2,1,7]
minimum-operations-to-make-the-array-increasing)maximum-product-after-k-increments)minimum-number-of-operations-to-make-elements-in-array-distinct)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #945: Minimum Increment to Make Array Unique
class Solution {
public int minIncrementForUnique(int[] nums) {
Arrays.sort(nums);
int ans = 0, y = -1;
for (int x : nums) {
y = Math.max(y + 1, x);
ans += y - x;
}
return ans;
}
}
// Accepted solution for LeetCode #945: Minimum Increment to Make Array Unique
func minIncrementForUnique(nums []int) (ans int) {
sort.Ints(nums)
y := -1
for _, x := range nums {
y = max(y+1, x)
ans += y - x
}
return
}
# Accepted solution for LeetCode #945: Minimum Increment to Make Array Unique
class Solution:
def minIncrementForUnique(self, nums: List[int]) -> int:
nums.sort()
ans, y = 0, -1
for x in nums:
y = max(y + 1, x)
ans += y - x
return ans
// Accepted solution for LeetCode #945: Minimum Increment to Make Array Unique
struct Solution;
impl Solution {
fn min_increment_for_unique(mut a: Vec<i32>) -> i32 {
let mut res = 0;
a.sort_unstable();
let mut prev: Option<i32> = None;
for x in a {
if let Some(y) = prev {
if x <= y {
res += y + 1 - x;
prev = Some(y + 1);
} else {
prev = Some(x);
}
} else {
prev = Some(x);
}
}
res
}
}
#[test]
fn test() {
let a = vec![1, 2, 2];
let res = 1;
assert_eq!(Solution::min_increment_for_unique(a), res);
let a = vec![3, 2, 1, 2, 1, 7];
let res = 6;
assert_eq!(Solution::min_increment_for_unique(a), res);
}
// Accepted solution for LeetCode #945: Minimum Increment to Make Array Unique
function minIncrementForUnique(nums: number[]): number {
nums.sort((a, b) => a - b);
let [ans, y] = [0, -1];
for (const x of nums) {
y = Math.max(y + 1, x);
ans += y - x;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.