-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2244.minimum-rounds-to-complete-all-tasks.cpp
More file actions
79 lines (78 loc) · 2.01 KB
/
2244.minimum-rounds-to-complete-all-tasks.cpp
File metadata and controls
79 lines (78 loc) · 2.01 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode id=2244 lang=cpp
*
* [2244] Minimum Rounds to Complete All Tasks
*
* https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/
*
* algorithms
* Medium (57.81%)
* Likes: 757
* Dislikes: 19
* Total Accepted: 42.1K
* Total Submissions: 69.1K
* Testcase Example: '[2,2,3,3,2,4,4,4,4,4]'
*
* 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 <= 10^5
* 1 <= tasks[i] <= 10^9
*
*
*/
// @lc code=start
// #include <bits/stdc++.h>
// using namespace std;
class Solution
{
public:
int minimumRounds(vector<int> &tasks)
{
int res = 0;
unordered_map<int, float> freqMap;
for (int x : tasks)
freqMap[x]++;
for (auto freqPair : freqMap)
{
if (freqPair.second == 1)
return -1;
res += ceil(freqPair.second / 3);
}
return res;
}
};
// @lc code=end