[LeetCode] Minimum Rounds to Complete All Tasks

2244. Minimum Rounds to Complete All Tasks

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int minimumRounds(vector<int>& tasks) {
unordered_map<int, int> mp;
for(auto& t : tasks)
mp[t]++;
int res = 0;
for(auto& [k, v] : mp) {
if(v == 1) return -1;
res += v / 3 + ((v % 3) > 0);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/17/PS/LeetCode/minimum-rounds-to-complete-all-tasks/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.