Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ tags:

<!-- solution:start -->

### 方法一:数学
### 方法一:计数

我们直接遍历数组 $\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)$。

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -111,10 +109,7 @@ public:
int minimumOperations(vector<int>& 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;
}
Expand All @@ -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
Expand All @@ -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>) -> i32 {
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ tags:

<!-- solution:start -->

### 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)$.

Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -109,10 +107,7 @@ public:
int minimumOperations(vector<int>& 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;
}
Expand All @@ -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
Expand All @@ -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>) -> i32 {
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ class Solution {
int minimumOperations(vector<int>& 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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
nums.iter().filter(|&&x| x % 3 != 0).count() as i32
}
}
Original file line number Diff line number Diff line change
@@ -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);
}