-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_Scheduler.cpp
More file actions
58 lines (48 loc) · 1.67 KB
/
Task_Scheduler.cpp
File metadata and controls
58 lines (48 loc) · 1.67 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
#include <vector>
#include <unordered_map>
#include <queue>
class Solution {
public:
int leastInterval(std::vector<char>& tasks, int n) {
// Get frequency of each task
std::unordered_map<char, int> freq;
for (int i=0; i<tasks.size(); i++) {
if (freq.find(tasks[i]) == freq.end()) {
freq.insert({tasks[i], 1});
} else {
freq[tasks[i]]++;
}
}
// Insert elements into a priority queue with most frequent tasks first
std::priority_queue<int> q;
for (auto i=freq.begin(); i!=freq.end(); i++) {
q.push(i->second);
}
// We create a queue to keep track of when a task is ready to be re-inserted
// each associated int is the timestep at which a task is ready to be re-inserted
std::queue<std::pair<int, int>> reQ;
int timeStep = 0;
while (!q.empty() || !reQ.empty()) {
// Process task if available
if (!q.empty()) {
int task = q.top();
q.pop();
// insert into insertion queue if tasks not all complete
if (task > 1) {
std::pair<char, int> toInsert = {task-1, timeStep + n};
reQ.push(toInsert);
}
}
// Re-insert from the insertion queue
if (!reQ.empty()) {
std::pair<int, int> nextTask = reQ.front();
if (nextTask.second == timeStep) {
q.push(nextTask.first);
reQ.pop();
}
}
timeStep++;
}
return timeStep;
}
};