diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md index 68fe4c358ac70..37d51ed213921 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md @@ -66,11 +66,16 @@ tags: -### 方法一:数学 +### 方法一:计数 -我们直接遍历数组 $\textit{nums}$,对于每个元素 $x$,我们计算 $x$ 除以 3 的余数 $x \bmod 3$,如果余数不为 0,我们需要将 $x$ 变为能被 3 整除且操作次数最少,那么我们可以选择将 $x$ 减少 $x \bmod 3$ 或者增加 $3 - x \bmod 3$,取两者的最小值累加到答案中。 +我们直接遍历数组 $\textit{nums}$,对于每个元素 $x$,如果 $x \bmod 3 \neq 0$,那么有两种情况: -时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 +- 如果 $x \bmod 3 = 1$,我们可以将 $x$ 减少 $1$,使其变为 $x - 1$,此时 $x - 1$ 可以被 $3$ 整除。 +- 如果 $x \bmod 3 = 2$,我们可以将 $x$ 增加 $1$,使其变为 $x + 1$,此时 $x + 1$ 可以被 $3$ 整除。 + +因此,我们只需要统计数组中不能被 $3$ 整除的元素个数,即可得到最少操作次数。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -79,11 +84,7 @@ tags: ```python class Solution: def minimumOperations(self, nums: List[int]) -> int: - ans = 0 - for x in nums: - if mod := x % 3: - ans += min(mod, 3 - mod) - return ans + return sum(x % 3 != 0 for x in nums) ``` #### Java @@ -93,10 +94,7 @@ class Solution { public int minimumOperations(int[] nums) { int ans = 0; for (int x : nums) { - int mod = x % 3; - if (mod != 0) { - ans += Math.min(mod, 3 - mod); - } + ans += x % 3 != 0 ? 1 : 0; } return ans; } @@ -111,10 +109,7 @@ public: int minimumOperations(vector& nums) { int ans = 0; for (int x : nums) { - int mod = x % 3; - if (mod) { - ans += min(mod, 3 - mod); - } + ans += x % 3 != 0 ? 1 : 0; } return ans; } @@ -126,8 +121,8 @@ public: ```go func minimumOperations(nums []int) (ans int) { for _, x := range nums { - if mod := x % 3; mod > 0 { - ans += min(mod, 3-mod) + if x%3 != 0 { + ans++ } } return @@ -138,14 +133,17 @@ func minimumOperations(nums []int) (ans int) { ```ts function minimumOperations(nums: number[]): number { - let ans = 0; - for (const x of nums) { - const mod = x % 3; - if (mod) { - ans += Math.min(mod, 3 - mod); - } + return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn minimum_operations(nums: Vec) -> i32 { + nums.iter().filter(|&&x| x % 3 != 0).count() as i32 } - return ans; } ``` diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md index 95003efe76f8d..6a97b2da16e7d 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md @@ -64,9 +64,14 @@ tags: -### Solution 1: Mathematics +### Solution 1: Counting -We directly iterate through the array $\textit{nums}$. For each element $x$, we calculate the remainder of $x$ divided by 3, $x \bmod 3$. If the remainder is not 0, we need to make $x$ divisible by 3 with the minimum number of operations. Therefore, we can choose to either decrease $x$ by $x \bmod 3$ or increase $x$ by $3 - x \bmod 3$, and we accumulate the minimum of these two values to the answer. +We directly iterate through the array $\textit{nums}$. For each element $x$, if $x \bmod 3 \neq 0$, there are two cases: + +- If $x \bmod 3 = 1$, we can decrease $x$ by $1$ to make it $x - 1$, which is divisible by $3$. +- If $x \bmod 3 = 2$, we can increase $x$ by $1$ to make it $x + 1$, which is divisible by $3$. + +Therefore, we only need to count the number of elements in the array that are not divisible by $3$ to get the minimum number of operations. The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -77,11 +82,7 @@ The time complexity is $O(n)$, where $n$ is the length of the array $\textit{num ```python class Solution: def minimumOperations(self, nums: List[int]) -> int: - ans = 0 - for x in nums: - if mod := x % 3: - ans += min(mod, 3 - mod) - return ans + return sum(x % 3 != 0 for x in nums) ``` #### Java @@ -91,10 +92,7 @@ class Solution { public int minimumOperations(int[] nums) { int ans = 0; for (int x : nums) { - int mod = x % 3; - if (mod != 0) { - ans += Math.min(mod, 3 - mod); - } + ans += x % 3 != 0 ? 1 : 0; } return ans; } @@ -109,10 +107,7 @@ public: int minimumOperations(vector& nums) { int ans = 0; for (int x : nums) { - int mod = x % 3; - if (mod) { - ans += min(mod, 3 - mod); - } + ans += x % 3 != 0 ? 1 : 0; } return ans; } @@ -124,8 +119,8 @@ public: ```go func minimumOperations(nums []int) (ans int) { for _, x := range nums { - if mod := x % 3; mod > 0 { - ans += min(mod, 3-mod) + if x%3 != 0 { + ans++ } } return @@ -136,14 +131,17 @@ func minimumOperations(nums []int) (ans int) { ```ts function minimumOperations(nums: number[]): number { - let ans = 0; - for (const x of nums) { - const mod = x % 3; - if (mod) { - ans += Math.min(mod, 3 - mod); - } + return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn minimum_operations(nums: Vec) -> i32 { + nums.iter().filter(|&&x| x % 3 != 0).count() as i32 } - return ans; } ``` diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.cpp b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.cpp index 58d4d914e47bf..8c486ed097126 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.cpp +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.cpp @@ -3,11 +3,8 @@ class Solution { int minimumOperations(vector& nums) { int ans = 0; for (int x : nums) { - int mod = x % 3; - if (mod) { - ans += min(mod, 3 - mod); - } + ans += x % 3 != 0 ? 1 : 0; } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.go b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.go index 35277cb3ff514..3fbd38082a211 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.go +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.go @@ -1,8 +1,8 @@ func minimumOperations(nums []int) (ans int) { for _, x := range nums { - if mod := x % 3; mod > 0 { - ans += min(mod, 3-mod) + if x%3 != 0 { + ans++ } } return -} \ No newline at end of file +} diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.java b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.java index 197c19d418f0f..c034743e8ca1d 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.java +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.java @@ -2,11 +2,8 @@ class Solution { public int minimumOperations(int[] nums) { int ans = 0; for (int x : nums) { - int mod = x % 3; - if (mod != 0) { - ans += Math.min(mod, 3 - mod); - } + ans += x % 3 != 0 ? 1 : 0; } return ans; } -} \ No newline at end of file +} diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.py b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.py index ef7eb636d74f2..20c169d8d67bf 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.py +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.py @@ -1,7 +1,3 @@ class Solution: def minimumOperations(self, nums: List[int]) -> int: - ans = 0 - for x in nums: - if mod := x % 3: - ans += min(mod, 3 - mod) - return ans + return sum(x % 3 != 0 for x in nums) diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.rs b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.rs new file mode 100644 index 0000000000000..d32022af379ed --- /dev/null +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn minimum_operations(nums: Vec) -> i32 { + nums.iter().filter(|&&x| x % 3 != 0).count() as i32 + } +} diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.ts b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.ts index a06092a878cb6..ea0f6d2b9816c 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.ts +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.ts @@ -1,10 +1,3 @@ function minimumOperations(nums: number[]): number { - let ans = 0; - for (const x of nums) { - const mod = x % 3; - if (mod) { - ans += Math.min(mod, 3 - mod); - } - } - return ans; + return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0); }