Skip to content

Commit 3f6dbad

Browse files
chore: add LeetCode daily solution
1 parent ed4e3f0 commit 3f6dbad

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Find Minimum Operations to Make All Elements Divisible by Three (Easy)
2+
3+
**Problem ID:** 3190
4+
**Date:** 2025-11-22
5+
**Link:** https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/
6+
7+
## Approach
8+
9+
To solve the problem of finding the minimum operations to make all elements of an integer array divisible by three, we can break down the approach into a few clear steps:
10+
11+
### Main Idea:
12+
The key insight is to recognize how each number's remainder when divided by 3 (i.e., `num % 3`) determines the number of operations needed to make it divisible by 3. Specifically:
13+
- If `num % 3 == 0`, no operations are needed.
14+
- If `num % 3 == 1`, we can either subtract 1 (1 operation) or add 2 (2 operations).
15+
- If `num % 3 == 2`, we can either subtract 2 (2 operations) or add 1 (1 operation).
16+
17+
### Steps:
18+
1. **Initialize a Counter**: Start with a counter set to zero to keep track of the total operations required.
19+
2. **Iterate through the Array**: For each element in the array, compute its remainder when divided by 3.
20+
3. **Update the Counter**: Based on the remainder:
21+
- If the remainder is 0, do nothing.
22+
- If the remainder is 1, add 1 to the counter (for subtraction) or 2 (for addition), but we choose the minimum which is 1.
23+
- If the remainder is 2, add 1 to the counter (for addition) or 2 (for subtraction), choosing the minimum which is also 1.
24+
4. **Return the Result**: After processing all elements, the counter will hold the minimum number of operations needed.
25+
26+
### Data Structures:
27+
- A simple integer counter is sufficient to keep track of the total operations. No complex data structures are needed due to the straightforward nature of the calculations.
28+
29+
### Complexity:
30+
- **Time Complexity**: O(n), where n is the length of the input array `nums`. We traverse the array once to compute the operations.
31+
- **Space Complexity**: O(1), as we only use a constant amount of space for the counter, regardless of the input size.
32+
33+
This approach is efficient and straightforward, leveraging modular arithmetic to minimize operations effectively.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int minOperations(int[] nums) {
3+
int operations = 0;
4+
for (int num : nums) {
5+
int remainder = num % 3;
6+
if (remainder != 0) {
7+
operations += Math.min(remainder, 3 - remainder);
8+
}
9+
}
10+
return operations;
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minOperations(nums) {
2+
let operations = 0;
3+
for (let num of nums) {
4+
const remainder = num % 3;
5+
if (remainder !== 0) {
6+
operations += Math.min(remainder, 3 - remainder);
7+
}
8+
}
9+
return operations;
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int]) -> int:
3+
operations = 0
4+
for num in nums:
5+
remainder = num % 3
6+
operations += min(remainder, 3 - remainder)
7+
return operations

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
286286
- 2025-11-19 — [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two/) (Easy) → `Easy/2025-11-19-2154-Keep-Multiplying-Found-Values-by-Two`
287287
- 2025-11-20 — [Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/) (Hard) → `Hard/2025-11-20-757-Set-Intersection-Size-At-Least-Two`
288288
- 2025-11-21 — [Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/unique-length-3-palindromic-subsequences/) (Medium) → `Medium/2025-11-21-1930-Unique-Length-3-Palindromic-Subsequences`
289+
- 2025-11-22 — [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/) (Easy) → `Easy/2025-11-22-3190-Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three`

0 commit comments

Comments
 (0)