-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path621.cpp
42 lines (34 loc) · 956 Bytes
/
621.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
// Date: 1/18/2020
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
// count
unordered_map<char, int> map;
for(const auto& t : tasks) map[t]++;
// priority_queue
priority_queue<pair<int, char>> pq;
for(auto& m : map) {
pq.push({m.second, m.first});
}
// cycle concept
int cycle = n + 1, alltime = 0;
while(!pq.empty()) {
int time = 0;
vector<pair<int, char>> tmp;
for(int i = 0; i < cycle; i++) {
if(!pq.empty()) {
tmp.push_back(pq.top());
pq.pop();
time++;
}
}
for(auto& t : tmp) {
if(--t.first > 0) {
pq.push(t);
}
}
alltime += pq.empty() ? time : cycle;
}
return alltime;
}
};