
At Stripe, an engineering team tracks repeated workload sizes in a batch job and wants to reduce processing overhead. Given an integer array tasks, return the minimum number of operations needed to remove all tasks, where one operation can remove either 2 equal values or 3 equal values.
If it is impossible to remove all tasks under these rules, return -1.
tasks — a list of integers-1 if no valid sequence existsExample 1
tasks = [2, 3, 3, 2, 2, 4, 2, 3, 4]42 three times as one group of 3 and one group of 1 is not valid, so use counts: 2 -> 4 needs 2 operations, 3 -> 3 needs 1 operation, 4 -> 2 needs 1 operation. Total = 4.Example 2
tasks = [2, 1, 2, 2, 3, 3]-11 appears once, and a single task cannot be removed.1 <= len(tasks) <= 10^51 <= tasks[i] <= 10^9tasks = [2, 3, 3, 2, 2, 4, 2, 3, 4]Output4WhyFrequencies are `2 -> 4`, `3 -> 3`, `4 -> 2`. These require `2`, `1`, and `1` operations respectively, for a total of `4`.tasks = [2, 1, 2, 2, 3, 3]Output-1WhyThe value `1` appears once, and no operation can remove a single task.tasks = [5, 5, 5, 5, 5, 5]Output2WhySix equal tasks can be removed as two groups of 3, which is optimal.1 <= len(tasks) <= 10^51 <= tasks[i] <= 10^9Each operation removes exactly 2 or 3 equal valuesdef min_operations(tasks):