diff --git a/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/README.md b/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/README.md index c03fb90e1..63440e544 100755 --- a/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/README.md +++ b/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/README.md @@ -1,28 +1,49 @@ # [3512.Minimum Operations to Make Array Sum Divisible by K][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` and an integer `k`. You can perform the following operation any number of times: + +- Select an index `i` and replace `nums[i]` with `nums[i] - 1`. + +Return the **minimum** number of operations required to make the sum of the array divisible by `k`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [3,9,7], k = 5 + +Output: 4 + +Explanation: + +Perform 4 operations on nums[1] = 9. Now, nums = [3, 5, 7]. +The sum is 15, which is divisible by 5. ``` -## 题意 -> ... +**Example 2:** + +``` +Input: nums = [4,1,3], k = 4 -## 题解 +Output: 0 + +Explanation: + +The sum is 8, which is already divisible by 4. Hence, no operations are needed. +``` + +**Example 3:** -### 思路1 -> ... -Minimum Operations to Make Array Sum Divisible by K -```go ``` +Input: nums = [3,2], k = 6 +Output: 5 + +Explanation: + +Perform 3 operations on nums[0] = 3 and 2 operations on nums[1] = 2. Now, nums = [0, 0]. +The sum is 0, which is divisible by 6. +``` ## 结语 diff --git a/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution.go b/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution.go index d115ccf5e..36d5d9125 100644 --- a/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution.go +++ b/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution.go @@ -1,5 +1,9 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, k int) int { + sum := 0 + for _, n := range nums { + sum += n + } + return sum % k } diff --git a/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution_test.go b/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution_test.go index 14ff50eb4..58d76f087 100644 --- a/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution_test.go +++ b/leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{3, 9, 7}, 5, 4}, + {"TestCase2", []int{4, 1, 3}, 4, 0}, + {"TestCase3", []int{3, 2}, 6, 5}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }