-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path621_Task-Scheduler.cpp
78 lines (66 loc) · 1.97 KB
/
621_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
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
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
// 1. Execute task in Priority Queue and put it into the Cooling Queue with task/release time(time+n)
// 2. Turn the task back to Priority Queue if release time is equal to the current time
priority_queue<int> pq;
queue<pair<int,int>> que;
int freq[26]={0};
for(char c:tasks) ++freq[c-'A'];
for(int count:freq){
if(!count) continue;
pq.push(count);
}
int time=0;
while(!pq.empty()||!que.empty()){
if(!pq.empty()){
if(pq.top()!=1){
pair<int,int>p;
p.first = pq.top()-1;
p.second = time + n;
que.push(p);
}
pq.pop(); // Executed
}
// Put the cooled task from Queue into Priority Qeue
if(!que.empty()&&que.front().second == time){
pq.push(que.front().first);
que.pop();
}
++time;
}
return time;
}
};
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int count[26] = {0};
for(const char &c : tasks){
++count[c-'A'];
}
priority_queue<int> maxHeap;
queue<pair<int,int>> q;
for(const int &ele : count){
if(ele == 0) continue;
maxHeap.push(ele);
}
int k = 0;
while(!maxHeap.empty() || !q.empty()){
if(maxHeap.empty()){
k = q.front().first+1;
}
if(!q.empty() && q.front().first < k){
maxHeap.push(q.front().second);
q.pop();
}
int task = maxHeap.top();
maxHeap.pop();
if(task > 1){
q.push(make_pair(k+n, task-1));
}
++k;
}
return k;
}
};