-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
48 lines (36 loc) · 1.18 KB
/
solution.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
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
if(n == 0) return tasks.size();
priority_queue<pair<int, char>> q;
unordered_map<char,int> map;
for(char task: tasks) {
map[task]++;
}
for(auto &x: map) {
q.push(make_pair(x.second, x.first));
}
int cooldown = n + 1;
int totalCPUTime = 0;
vector<pair<int, char>> rest;
while(!q.empty()) {
while(cooldown > 0 && q.size() > 0) {
pair<int, char> currentTop = q.top();
q.pop();
cooldown--;
totalCPUTime++;
currentTop.first -= 1;
if(currentTop.first > 0) rest.push_back(currentTop);
}
for(pair<int, char> r: rest) {
q.push(r);
}
if(q.size() > 0) {
totalCPUTime += cooldown;
}
rest.clear();
cooldown = n + 1;
}
return totalCPUTime;
}
};