You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README_EN.md
+60-5
Original file line number
Diff line number
Diff line change
@@ -47,12 +47,26 @@ It can be shown that we cannot make the array empty in less than 4 operations.
47
47
48
48
## Solutions
49
49
50
+
**Solution 1: Hash Table + Greedy**
51
+
52
+
We use a hash table $count$ to count the number of occurrences of each element in the array. Then we traverse the hash table. For each element $x$, if it appears $c$ times, we can perform $\lfloor \frac{c+2}{3} \rfloor$ operations to delete $x$. Finally, we return the sum of the number of operations for all elements.
53
+
54
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(n)$.
0 commit comments