-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTask_Scheduler.cpp
35 lines (34 loc) · 981 Bytes
/
Task_Scheduler.cpp
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
// round robin
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
unordered_map<char, int> taskFreq;
for (char task : tasks) {
taskFreq[task]++;
}
priority_queue<pair<int, char>> pQ;
for (pair<char, int> taskCount : taskFreq) {
pQ.push({taskCount.second, taskCount.first});
}
int interval = 0;
int cycle = n + 1;
while(!pQ.empty()) {
int tasks = 0;
vector<pair<int, char>> tmp;
for(int i = 0; i < cycle; i++) {
if(!pQ.empty()) {
tmp.push_back(pQ.top());
pQ.pop();
tasks++;
}
}
for(auto task : tmp) {
if(--task.first) {
pQ.push(task);
}
}
interval += !pQ.empty() ? cycle : tasks;
}
return interval;
}
};