Skip to content

Commit 43874bb

Browse files
chore: add LeetCode daily solution
1 parent c58a773 commit 43874bb

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Minimum Operations to Convert All Elements to Zero (Medium)
2+
3+
**Problem ID:** 3542
4+
**Date:** 2025-11-10
5+
**Link:** https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/
6+
7+
## Approach
8+
9+
To solve the problem of converting all elements in the array to zero with the minimum number of operations, we can follow a systematic approach:
10+
11+
### Main Idea:
12+
The key observation is that each unique non-negative integer in the array can be treated independently. By identifying the unique integers and their occurrences, we can minimize the number of operations required to set all elements to zero.
13+
14+
### Steps to Solve the Problem:
15+
16+
1. **Identify Unique Elements**: First, we need to determine the unique non-negative integers present in the array. This can be efficiently done using a set or a dictionary to track occurrences.
17+
18+
2. **Count Operations**: For each unique integer, we need to perform operations to set all its occurrences to zero. The number of operations required is equal to the number of unique integers present in the array. This is because we can always select a subarray that contains all occurrences of a particular integer and set them to zero in one operation.
19+
20+
3. **Return Result**: The final result is simply the count of unique non-negative integers, as each will require one operation to be set to zero.
21+
22+
### Data Structures:
23+
- **Set or Dictionary**: To store unique integers and count them efficiently. This allows for O(1) average time complexity for insertions and lookups.
24+
25+
### Complexity Analysis:
26+
- **Time Complexity**: O(n), where n is the length of the input array. This is due to the need to iterate through the array to collect unique integers.
27+
- **Space Complexity**: O(k), where k is the number of unique integers in the array. In the worst case, this could be O(n) if all elements are unique.
28+
29+
### Conclusion:
30+
By leveraging the properties of unique integers and their occurrences, we can efficiently determine the minimum number of operations required to convert all elements to zero. This approach is optimal and scales well with the input size constraints.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class Solution {
5+
public int minimumOperations(int[] nums) {
6+
Set<Integer> uniqueNonZero = new HashSet<>();
7+
for (int num : nums) {
8+
if (num != 0) {
9+
uniqueNonZero.add(num);
10+
}
11+
}
12+
return uniqueNonZero.size();
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var minimumOperations = function(nums) {
2+
const uniqueNums = new Set(nums);
3+
uniqueNums.delete(0);
4+
return uniqueNums.size;
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def minimumOperations(self, nums: List[int]) -> int:
3+
unique_nums = set(nums)
4+
unique_nums.discard(0)
5+
return len(unique_nums)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
274274
- 2025-11-07 — [Maximize the Minimum Powered City](https://leetcode.com/problems/maximize-the-minimum-powered-city/) (Hard) → `Hard/2025-11-07-2528-Maximize-the-Minimum-Powered-City`
275275
- 2025-11-08 — [Minimum One Bit Operations to Make Integers Zero](https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/) (Hard) → `Hard/2025-11-08-1611-Minimum-One-Bit-Operations-to-Make-Integers-Zero`
276276
- 2025-11-09 — [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero/) (Easy) → `Easy/2025-11-09-2169-Count-Operations-to-Obtain-Zero`
277+
- 2025-11-10 — [Minimum Operations to Convert All Elements to Zero](https://leetcode.com/problems/minimum-operations-to-convert-all-elements-to-zero/) (Medium) → `Medium/2025-11-10-3542-Minimum-Operations-to-Convert-All-Elements-to-Zero`

0 commit comments

Comments
 (0)