From 884123bdf9537c3bb179fe29d8f8ecc73ccb1332 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 22 Nov 2025 22:23:28 +0800 Subject: [PATCH] Add solution and test-cases for problem 3190 --- .../README.md | 31 +++++++++++-------- .../Solution.go | 18 +++++++++-- .../Solution_test.go | 13 ++++---- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/README.md b/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/README.md index a703bb935..e7227f2cb 100755 --- a/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/README.md +++ b/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/README.md @@ -1,28 +1,33 @@ # [3190.Find Minimum Operations to Make All Elements Divisible by Three][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 operation, you can add or subtract 1 from **any** element of `nums`. + +Return the **minimum** number of operations to make all elements of `nums` divisible by 3. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [1,2,3,4] + +Output: 3 -## 题意 -> ... +Explanation: -## 题解 +All array elements can be made divisible by 3 using 3 operations: -### 思路1 -> ... -Find Minimum Operations to Make All Elements Divisible by Three -```go +Subtract 1 from 1. +Add 1 to 2. +Subtract 1 from 4. ``` +**Example 2:** + +``` +Input: nums = [3,6,9] + +Output: 0 +``` ## 结语 diff --git a/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution.go b/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution.go index d115ccf5e..c103bacf4 100644 --- a/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution.go +++ b/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int) int { + + byThree := []int{0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51} + l := len(byThree) + var ret int + + for _, n := range nums { + index := sort.Search(l, func(i int) bool { + return byThree[i] >= n + }) + ret += min(byThree[index]-n, n-byThree[index-1]) + } + + return ret } diff --git a/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution_test.go b/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution_test.go index 14ff50eb4..e2a9bd120 100644 --- a/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution_test.go +++ b/leetcode/3101-3200/3190.Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three/Solution_test.go @@ -10,12 +10,11 @@ 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, 3, 4}, 3}, + {"TestCase2", []int{3, 6, 9}, 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }