Skip to content

Commit

Permalink
Merge pull request #784 from 0xff-dev/945
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 945
  • Loading branch information
6boris committed Mar 23, 2024
2 parents 9a12949 + 409735b commit 54e0718
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# [945.Minimum Increment to Make Array Unique][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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: a = "11", b = "1"
Output: "100"
Input: nums = [1,2,2]
Output: 1
Explanation: After 1 move, the array could be [1, 2, 3].
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Minimum Increment to Make Array Unique
```go
```

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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
count := make(map[int]int)
mn := -1
for _, n := range nums {
count[n]++
if mn == -1 || mn > n {
mn = n
}
}
need := len(nums)
steps := 0
cur := mn
for need > 0 {
if count[cur] == 1 || count[cur] == 0 {
if count[cur] != 0 {
need--
}
cur++
continue
}
next := cur + 1
step := count[cur] - 1
steps += step
count[next] += step
cur = next
need--
}
return steps
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 2}, 1},
{"TestCase2", []int{3, 2, 1, 2, 1, 7}, 6},
{"TestCase3", []int{1, 2, 3, 3, 3, 3, 2, 4, 4, 4, 45, 5}, 32},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 54e0718

Please sign in to comment.