-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0945.go
52 lines (45 loc) · 1.4 KB
/
0945.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Source: https://leetcode.com/problems/minimum-increment-to-make-array-unique
// Title: Minimum Increment to Make Array Unique
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 with 5 or less moves that it is impossible for the array to have all unique values.
//
// Constraints:
//
// 1 <= nums.length <= 10^5
// 0 <= nums[i] <= 10^5
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import "slices"
func minIncrementForUnique(nums []int) int {
// Sort
slices.Sort(nums)
target := 0
res := 0
for _, num := range nums {
target = max(target, num)
res += target - num
target++
}
return res
}