<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers.</p>

<p>There are two types of operations that you can apply on the array <strong>any</strong> number of times:</p>

<ul>
	<li>Choose <strong>two</strong> elements with <strong>equal</strong> values and <strong>delete</strong> them from the array.</li>
	<li>Choose <strong>three</strong> elements with <strong>equal</strong> values and <strong>delete</strong> them from the array.</li>
</ul>

<p>Return <em>the <strong>minimum</strong> number of operations required to make the array empty, or </em><code>-1</code><em> if it is not possible</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,3,2,2,4,2,3,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can apply the following operations to make the array empty:
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
It can be shown that we cannot make the array empty in less than 4 operations.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,2,2,3,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> It is impossible to empty the array.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/" target="_blank">2244: Minimum Rounds to Complete All Tasks.</a></p>

class Solution:
    def minOperations(self, nums: List[int]) -> int:

        freq_map = {}
        for n in nums:
            freq_map[n]= freq_map.get(n, 0)+1
        


        def dp_cnt(freq):
            
            dp=[freq+1] * (freq+1)
            dp[0]=0
            dp[1]=-1

            for op in range(2,4):
                for j in range(1, freq+1):
                    if j >= op:
                        if dp[j-op] >= 0 and dp[j] !=-1:
                            dp[j]= min(1+dp[j-op], dp[j])
                        elif dp[j-op] >= 0 and dp[j] == -1:
                            dp[j]= 1+dp[j-op]
      
            return dp[-1]

        total=0
        for key, val in freq_map.items():
            op = dp_cnt(val)
            if op==-1:
                return -1
            else:
                total+=op

        return total


'''                     else:

part 1: count frequency for each unique number:
    - 1, not posiible
    - 2, 1 del
    - 3, 1 del
    - 4. 2 dels
    - 5. [2,3] 1 del
    - 6. [3,3]. [2,2,2]. 2 dels
    - 7. [2,2,3] [3,2,2]  

total_len
0[0,0,0,0,0,0,0]
2 0 1,0,2,0,3,0
3 0,0,1,0,2,2,3
'''
30500