Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int leastInterval(vector<char>& tasks, int n) {
- map<char,int> m;
- map<char,int> hold;
- for(char c:tasks){
- m[c]++;
- }
- priority_queue<pair<char,int>> q;
- for(auto iter:m){
- q.push(make_pair(iter.first,iter.second));
- }
- int ans=0;
- while(!q.empty()){
- priority_queue<pair<char,int>> temp;
- for(int i=0;i<q.size();i++){
- pair<char,int> t=q.top();
- q.pop();
- if(hold[t.first]<=0){
- hold[t.first]=n;
- t.second--;
- if(t.second!=0){
- temp.push(t);
- }
- break;
- }else{
- temp.push(t);
- }
- }
- ans++;
- q=temp;
- while(!temp.empty()){
- cout<<temp.top().first<<":"<<temp.top().second<<":"<<hold[temp.top().first]<<' ';
- temp.pop();
- }
- cout<<endl;
- for(auto iter:hold){
- iter.second--;
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment