-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path621. Task Scheduler.cpp
More file actions
43 lines (42 loc) · 1.13 KB
/
621. Task Scheduler.cpp
File metadata and controls
43 lines (42 loc) · 1.13 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
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
map<char, int> mp;
for(auto x : tasks)
mp[x]++;
priority_queue<pair<int, char>> pq;
for(auto x : mp)
{
pq.push({x.second, x.first});
}
int cnt =tasks.size(), m =tasks.size();
while(!pq.empty())
{
priority_queue<pair<int, char>> tmp;
for(int i=0;i<=n;i++)
{
if(!pq.empty())
{
pair<int, char> p = pq.top();
pq.pop();
// cout<<p.second<<" ";
if(p.first>1)
tmp.push({p.first-1, p.second});
m--;
}else if(m){
//cout<<n-i+1<<" ";
cnt += n-i+1;
break;
}
}
while(!tmp.empty())
{
pair<int, char> p = tmp.top();
tmp.pop();
pq.push({p.first, p.second});
}
}
cout<<endl;
return cnt;
}
};