You are given a 0-indexed integer array tasks
, where tasks[i]
represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level. Return the minimum rounds required to complete all the tasks, or -1
if it is not possible to complete all the tasks.
Example 1:
1 2 3 4 5 6 7 8 |
<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 |
1 |
https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class Solution { public int minimumRounds(int[] tasks) { //if length is less than or equals to 1 return -1 if(tasks.length<=1) return -1; Map<Integer, Integer> map = new HashMap<>(); //create a frequency map for(int task: tasks){ map.put(task, map.getOrDefault(task,0)+1); } int sum = 0; for(int count: map.values()){ //if frequenc is 1 return -1 if(count==1) return -1; //if the frequency is divisible by 3 if(count%3==0){ sum = sum + count/3; } //example count = 22 else if(count%3==1){ sum = sum + (count-4)/3+2; } //example count = 17 else if(count%3==2){ sum = sum + (count-2)/3+1; } } return sum; } } |