2023-01-04 Daily Challenge
Today I have done leetcode's January LeetCoding Challenge with cpp
.
January LeetCoding Challenge 4
Description
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.
Example 1:
Input: tasks = [2,2,3,3,2,4,4,4,4,4] Output: 4 Explanation: 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.
Example 2:
Input: tasks = [2,3,3] Output: -1 Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.
Constraints:
1 <= tasks.length <= 105
1 <= tasks[i] <= 109
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
int minimumRounds(vector<int>& tasks) {
map<int, int> count;
for(auto task : tasks) {
count[task] += 1;
}
int answer = 0;
for(const auto &[_, c] : count) {
if(c == 1) return -1;
int result = INT_MAX;
if(c % 2 == 0) {
result = c / 2;
}
if((c - 4 > 0 && (c - 4) % 3 == 0) || (c - 2 > 0 && (c - 2) % 3 == 0)) {
result = c / 3 + 1;
}
if(c % 3 == 0) {
result = c / 3;
}
answer += result;
}
return answer;
}
};
// Accepted
// 79/79 cases passed (276 ms)
// Your runtime beats 66.53 % of cpp submissions
// Your memory usage beats 23.86 % of cpp submissions (104.7 MB)