Skip to content

Commit 8c25d87

Browse files
committed
Add solution and test-cases for problem 3512
1 parent f03226b commit 8c25d87

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/README.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# [3512.Minimum Operations to Make Array Sum Divisible by K][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums` and an integer `k`. You can perform the following operation any number of times:
5+
6+
- Select an index `i` and replace `nums[i]` with `nums[i] - 1`.
7+
8+
Return the **minimum** number of operations required to make the sum of the array divisible by `k`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [3,9,7], k = 5
14+
15+
Output: 4
16+
17+
Explanation:
18+
19+
Perform 4 operations on nums[1] = 9. Now, nums = [3, 5, 7].
20+
The sum is 15, which is divisible by 5.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
24+
25+
```
26+
Input: nums = [4,1,3], k = 4
1727
18-
## 题解
28+
Output: 0
29+
30+
Explanation:
31+
32+
The sum is 8, which is already divisible by 4. Hence, no operations are needed.
33+
```
34+
35+
**Example 3:**
1936

20-
### 思路1
21-
> ...
22-
Minimum Operations to Make Array Sum Divisible by K
23-
```go
2437
```
38+
Input: nums = [3,2], k = 6
2539
40+
Output: 5
41+
42+
Explanation:
43+
44+
Perform 3 operations on nums[0] = 3 and 2 operations on nums[1] = 2. Now, nums = [0, 0].
45+
The sum is 0, which is divisible by 6.
46+
```
2647

2748
## 结语
2849

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, k int) int {
4+
sum := 0
5+
for _, n := range nums {
6+
sum += n
7+
}
8+
return sum % k
59
}

leetcode/3501-3600/3512.Minimum-Operations-to-Make-Array-Sum-Divisible-by-K/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{3, 9, 7}, 5, 4},
18+
{"TestCase2", []int{4, 1, 3}, 4, 0},
19+
{"TestCase3", []int{3, 2}, 6, 5},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)