-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTask Scheduler.cpp
More file actions
43 lines (32 loc) · 951 Bytes
/
Copy pathTask Scheduler.cpp
File metadata and controls
43 lines (32 loc) · 951 Bytes
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
class Solution {
public:
int leastInterval(const std::vector<char>& tasks, int n) {
if(tasks.size() == 0)
return 0;
std::priority_queue<int> maxHeap; //default maxHeap
std::queue<std::pair<int,int>>instructionQueue;
std::unordered_map<char,int>insKeys;
for(char i : tasks)
insKeys[i]+=1;
for(const auto & pair : insKeys)
maxHeap.push(pair.second);
size_t time = 1; //ya i start with one
while(!maxHeap.empty() || !instructionQueue.empty()){
if(!instructionQueue.empty()){
const auto& pair = instructionQueue.front();
if(pair.second == time){
maxHeap.push(pair.first);
instructionQueue.pop();
}
}
if(!maxHeap.empty()){
int elm = maxHeap.top();
if(elm - 1 != 0)
instructionQueue.push(std::make_pair(elm-1,time + n + 1));
maxHeap.pop();
}
time++;
}
return --time;
}
};