描述
给你一个下标从 0 开始的整数数组 tasks ,其中 tasks[i] 表示任务的难度级别。在每一轮中,你可以完成 2 个或者 3 个 相同难度级别 的任务。
返回完成所有任务需要的 最少 轮数,如果无法完成所有任务,返回 -1
class Solution {
public int minimumRounds(int[] tasks) {
int sum = 0;
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < tasks.length; i++){
int tmp = map.getOrDefault(tasks[i],0);
map.put(tasks[i],tmp+1);
}
for(int m : map.keySet()){
int count = map.get(m);
if(count < 2){
return -1;
}
if(count % 3 == 0){
sum += count / 3 ;
}else {
sum += count / 3 + 1;
}
}
return sum;
}
}