Skip to content

Commit ef93b6d

Browse files
committed
feat: add solutions to lc problem: No.3190
1 parent 46325f9 commit ef93b6d

File tree

8 files changed

+59
-75
lines changed

8 files changed

+59
-75
lines changed

solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### 方法一:数学
69+
### 方法一:计数
7070

71-
我们直接遍历数组 $\textit{nums}$,对于每个元素 $x$,我们计算 $x$ 除以 3 的余数 $x \bmod 3$,如果余数不为 0,我们需要将 $x$ 变为能被 3 整除且操作次数最少,那么我们可以选择将 $x$ 减少 $x \bmod 3$ 或者增加 $3 - x \bmod 3$,取两者的最小值累加到答案中。
71+
我们直接遍历数组 $\textit{nums}$,对于每个元素 $x$,如果 $x \bmod 3 \neq 0$,那么有两种情况:
7272

73-
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
73+
- 如果 $x \bmod 3 = 1$,我们可以将 $x$ 减少 $1$,使其变为 $x - 1$,此时 $x - 1$ 可以被 $3$ 整除。
74+
- 如果 $x \bmod 3 = 2$,我们可以将 $x$ 增加 $1$,使其变为 $x + 1$,此时 $x + 1$ 可以被 $3$ 整除。
75+
76+
因此,我们只需要统计数组中不能被 $3$ 整除的元素个数,即可得到最少操作次数。
77+
78+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
7479

7580
<!-- tabs:start -->
7681

@@ -79,11 +84,7 @@ tags:
7984
```python
8085
class Solution:
8186
def minimumOperations(self, nums: List[int]) -> int:
82-
ans = 0
83-
for x in nums:
84-
if mod := x % 3:
85-
ans += min(mod, 3 - mod)
86-
return ans
87+
return sum(x % 3 != 0 for x in nums)
8788
```
8889

8990
#### Java
@@ -93,10 +94,7 @@ class Solution {
9394
public int minimumOperations(int[] nums) {
9495
int ans = 0;
9596
for (int x : nums) {
96-
int mod = x % 3;
97-
if (mod != 0) {
98-
ans += Math.min(mod, 3 - mod);
99-
}
97+
ans += x % 3 != 0 ? 1 : 0;
10098
}
10199
return ans;
102100
}
@@ -111,10 +109,7 @@ public:
111109
int minimumOperations(vector<int>& nums) {
112110
int ans = 0;
113111
for (int x : nums) {
114-
int mod = x % 3;
115-
if (mod) {
116-
ans += min(mod, 3 - mod);
117-
}
112+
ans += x % 3 != 0 ? 1 : 0;
118113
}
119114
return ans;
120115
}
@@ -126,8 +121,8 @@ public:
126121
```go
127122
func minimumOperations(nums []int) (ans int) {
128123
for _, x := range nums {
129-
if mod := x % 3; mod > 0 {
130-
ans += min(mod, 3-mod)
124+
if x%3 != 0 {
125+
ans++
131126
}
132127
}
133128
return
@@ -138,14 +133,17 @@ func minimumOperations(nums []int) (ans int) {
138133

139134
```ts
140135
function minimumOperations(nums: number[]): number {
141-
let ans = 0;
142-
for (const x of nums) {
143-
const mod = x % 3;
144-
if (mod) {
145-
ans += Math.min(mod, 3 - mod);
146-
}
136+
return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0);
137+
}
138+
```
139+
140+
#### Rust
141+
142+
```rust
143+
impl Solution {
144+
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
145+
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
147146
}
148-
return ans;
149147
}
150148
```
151149

solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1: Mathematics
67+
### Solution 1: Counting
6868

69-
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.
69+
We directly iterate through the array $\textit{nums}$. For each element $x$, if $x \bmod 3 \neq 0$, there are two cases:
70+
71+
- If $x \bmod 3 = 1$, we can decrease $x$ by $1$ to make it $x - 1$, which is divisible by $3$.
72+
- If $x \bmod 3 = 2$, we can increase $x$ by $1$ to make it $x + 1$, which is divisible by $3$.
73+
74+
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.
7075

7176
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
7277

@@ -77,11 +82,7 @@ The time complexity is $O(n)$, where $n$ is the length of the array $\textit{num
7782
```python
7883
class Solution:
7984
def minimumOperations(self, nums: List[int]) -> int:
80-
ans = 0
81-
for x in nums:
82-
if mod := x % 3:
83-
ans += min(mod, 3 - mod)
84-
return ans
85+
return sum(x % 3 != 0 for x in nums)
8586
```
8687

8788
#### Java
@@ -91,10 +92,7 @@ class Solution {
9192
public int minimumOperations(int[] nums) {
9293
int ans = 0;
9394
for (int x : nums) {
94-
int mod = x % 3;
95-
if (mod != 0) {
96-
ans += Math.min(mod, 3 - mod);
97-
}
95+
ans += x % 3 != 0 ? 1 : 0;
9896
}
9997
return ans;
10098
}
@@ -109,10 +107,7 @@ public:
109107
int minimumOperations(vector<int>& nums) {
110108
int ans = 0;
111109
for (int x : nums) {
112-
int mod = x % 3;
113-
if (mod) {
114-
ans += min(mod, 3 - mod);
115-
}
110+
ans += x % 3 != 0 ? 1 : 0;
116111
}
117112
return ans;
118113
}
@@ -124,8 +119,8 @@ public:
124119
```go
125120
func minimumOperations(nums []int) (ans int) {
126121
for _, x := range nums {
127-
if mod := x % 3; mod > 0 {
128-
ans += min(mod, 3-mod)
122+
if x%3 != 0 {
123+
ans++
129124
}
130125
}
131126
return
@@ -136,14 +131,17 @@ func minimumOperations(nums []int) (ans int) {
136131

137132
```ts
138133
function minimumOperations(nums: number[]): number {
139-
let ans = 0;
140-
for (const x of nums) {
141-
const mod = x % 3;
142-
if (mod) {
143-
ans += Math.min(mod, 3 - mod);
144-
}
134+
return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0);
135+
}
136+
```
137+
138+
#### Rust
139+
140+
```rust
141+
impl Solution {
142+
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
143+
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
145144
}
146-
return ans;
147145
}
148146
```
149147

solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ class Solution {
33
int minimumOperations(vector<int>& nums) {
44
int ans = 0;
55
for (int x : nums) {
6-
int mod = x % 3;
7-
if (mod) {
8-
ans += min(mod, 3 - mod);
9-
}
6+
ans += x % 3 != 0 ? 1 : 0;
107
}
118
return ans;
129
}
13-
};
10+
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
func minimumOperations(nums []int) (ans int) {
22
for _, x := range nums {
3-
if mod := x % 3; mod > 0 {
4-
ans += min(mod, 3-mod)
3+
if x%3 != 0 {
4+
ans++
55
}
66
}
77
return
8-
}
8+
}

solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/Solution.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ class Solution {
22
public int minimumOperations(int[] nums) {
33
int ans = 0;
44
for (int x : nums) {
5-
int mod = x % 3;
6-
if (mod != 0) {
7-
ans += Math.min(mod, 3 - mod);
8-
}
5+
ans += x % 3 != 0 ? 1 : 0;
96
}
107
return ans;
118
}
12-
}
9+
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
class Solution:
22
def minimumOperations(self, nums: List[int]) -> int:
3-
ans = 0
4-
for x in nums:
5-
if mod := x % 3:
6-
ans += min(mod, 3 - mod)
7-
return ans
3+
return sum(x % 3 != 0 for x in nums)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
3+
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
4+
}
5+
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
function minimumOperations(nums: number[]): number {
2-
let ans = 0;
3-
for (const x of nums) {
4-
const mod = x % 3;
5-
if (mod) {
6-
ans += Math.min(mod, 3 - mod);
7-
}
8-
}
9-
return ans;
2+
return nums.reduce((acc, x) => acc + (x % 3 !== 0 ? 1 : 0), 0);
103
}

0 commit comments

Comments
 (0)