Skip to content

Commit a17cab4

Browse files
authored
Merge pull request #1378 from 0xff-dev/3190
Add solution and test-cases for problem 3190
2 parents 14f2ba8 + 884123b commit a17cab4

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [3190.Find Minimum Operations to Make All Elements Divisible by Three][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`. In one operation, you can add or subtract 1 from **any** element of `nums`.
5+
6+
Return the **minimum** number of operations to make all elements of `nums` divisible by 3.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
11+
Input: nums = [1,2,3,4]
12+
13+
Output: 3
1414
15-
## 题意
16-
> ...
15+
Explanation:
1716
18-
## 题解
17+
All array elements can be made divisible by 3 using 3 operations:
1918
20-
### 思路1
21-
> ...
22-
Find Minimum Operations to Make All Elements Divisible by Three
23-
```go
19+
Subtract 1 from 1.
20+
Add 1 to 2.
21+
Subtract 1 from 4.
2422
```
2523

24+
**Example 2:**
25+
26+
```
27+
Input: nums = [3,6,9]
28+
29+
Output: 0
30+
```
2631

2732
## 结语
2833

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) int {
6+
7+
byThree := []int{0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51}
8+
l := len(byThree)
9+
var ret int
10+
11+
for _, n := range nums {
12+
index := sort.Search(l, func(i int) bool {
13+
return byThree[i] >= n
14+
})
15+
ret += min(byThree[index]-n, n-byThree[index-1])
16+
}
17+
18+
return ret
519
}

leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution_test.go

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)