FunctionRacerProblemsRace

Fewest Increments to Distinct Values

MediumGreedyPython, JavaScript, TypeScript, Java, C++

In one move you add 1 to any single item of nums. Return the fewest moves that make every value in nums different from every other value. Items are never decreased. An empty list or a list with one item needs 0 moves.

Example 1

nums [1,2,2]
returns 1

Raise one of the 2s to 3: one move.

Example 2

nums [3,2,1,2,1,7]
returns 6

Sorted, the values are 1, 1, 2, 2, 3, 7. Turning them into 1, 2, 3, 4, 5, 7 costs 0 + 1 + 1 + 2 + 2 + 0 = 6 moves, and nothing cheaper works.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems