<p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>

<p>Return <em>the <strong>minimum</strong> rounds required to complete all the tasks, or </em><code>-1</code><em> if it is not possible to complete all the tasks.</em></p>

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

<pre>
<strong>Input:</strong> tasks = [2,2,3,3,2,4,4,4,4,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> To complete all the tasks, a possible plan is:
- In the first round, you complete 3 tasks of difficulty level 2. 
- In the second round, you complete 2 tasks of difficulty level 3. 
- In the third round, you complete 3 tasks of difficulty level 4. 
- In the fourth round, you complete 2 tasks of difficulty level 4.  
It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
</pre>

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

<pre>
<strong>Input:</strong> tasks = [2,3,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.
</pre>

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

<ul>
	<li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= tasks[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/description/" target="_blank">2870: Minimum Number of Operations to Make Array Empty.</a></p>


class Solution 
{
public:
    int minimumRounds(vector<int>& tasks) 
    {
        unordered_map<int, int> mp;
        
       
        for (auto & x: tasks) 
        {
            mp[x]++;
        }
        
        int maxFreq = 0;
        for (auto& x : mp) 
        {
            maxFreq = max(maxFreq, x.second);
        }
        
       
        vector<int> dp(maxFreq + 1, INT_MAX);
        
      
        dp[0] = 0;  
        dp[1] = -1; 
        if (maxFreq >= 2) dp[2] = 1; 
        if (maxFreq >= 3) dp[3] = 1;  
        
        // Fill DP array
        for (int i = 4; i <= maxFreq; i++) {
            if (dp[i - 2] != -1) 
            {
                dp[i] = min(dp[i], dp[i - 2] + 1);
            }    
            if (dp[i - 3] != -1)
            {
                dp[i] = min(dp[i], dp[i - 3] + 1);

            } 
            
          
            if (dp[i] == INT_MAX)
            {
                dp[i] = -1;
            } 
        }
        
        int totalRounds = 0;
        
      
        for (auto& x : mp) {
            int freq = x.second;
            if (dp[freq] == -1) return -1; 
            totalRounds += dp[freq];
        }
        
        return totalRounds;
    }
};



109300