2244. Minimum Rounds to Complete All Tasks

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
class Solution {
public int minimumRounds(int[] tasks) {
Map<Integer, Integer> difficultyLevelToFreqMap = new HashMap<>();
for (int task : tasks) {
difficultyLevelToFreqMap.put(task, difficultyLevelToFreqMap.getOrDefault(task, 0) + 1);
}

int minRounds = 0;
for (int freq : difficultyLevelToFreqMap.values()) {
if (freq == 1) {
return -1;
} else {
int remainder = freq % 3; // [0, 1, 2]
if (remainder == 0) {
minRounds += freq / 3;
} else if (remainder == 1) {
minRounds += (freq - 4) / 3 + 2;
} else {
// reminder == 2
minRounds += freq / 3 + 1;
}
}
}

return minRounds;
}
}

References

2244. Minimum Rounds to Complete All Tasks