From 409735be82ecbf52d8be1f6b97011e68b738183e Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 14 Feb 2024 10:22:41 +0800 Subject: [PATCH] Add solution and test-cases for problem 945 --- .../README.md | 28 ++++++++--------- .../Solution.go | 30 +++++++++++++++++-- .../Solution_test.go | 14 ++++----- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/README.md b/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/README.md index c721e0095..b57e15f3d 100644 --- a/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/README.md +++ b/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution.go b/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution.go index d115ccf5e..5774ee365 100644 --- a/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution.go +++ b/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution_test.go b/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution_test.go index 14ff50eb4..d4b20f4aa 100644 --- a/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution_test.go +++ b/leetcode/901-1000/0945.Minimum-Increment-to-Make-Array-Unique/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }