方法:模拟
class Solution {
public int minimumRounds(int[] tasks) {
Map<Integer, Integer> map = new HashMap<>();
for (int task : tasks) {
map.put(task, map.getOrDefault(task, 0) + 1);
}
int ans = 0;
for (int count : map.values()) {
//发现规律:count % 3余数为0, 1, 2时
//比如6, 4, 5,需要的最少轮数是一样的
if (count == 1) {
return -1;
} else if (count == 2 || count == 3) {
ans++;
} else if (count % 3 == 0) {
ans += count / 3;
} else {
ans += count / 3 + 1;
}
//以上代码可以简化成:
// if (count == 1) return -1;
// ans += (count + 2) / 3;
}
return ans;
}
}