jibha

Untitled

Feb 3rd, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int leastInterval(vector<char>& tasks, int n) {
  4. map<char,int> m;
  5. map<char,int> hold;
  6.  
  7.  
  8. for(char c:tasks){
  9. m[c]++;
  10. }
  11.  
  12. priority_queue<pair<char,int>> q;
  13.  
  14. for(auto iter:m){
  15. q.push(make_pair(iter.first,iter.second));
  16. }
  17.  
  18. int ans=0;
  19. while(!q.empty()){
  20.  
  21. priority_queue<pair<char,int>> temp;
  22. for(int i=0;i<q.size();i++){
  23. pair<char,int> t=q.top();
  24. q.pop();
  25.  
  26. if(hold[t.first]<=0){
  27. hold[t.first]=n;
  28. t.second--;
  29. if(t.second!=0){
  30. temp.push(t);
  31. }
  32. break;
  33. }else{
  34. temp.push(t);
  35. }
  36. }
  37.  
  38. ans++;
  39. q=temp;
  40.  
  41. while(!temp.empty()){
  42. cout<<temp.top().first<<":"<<temp.top().second<<":"<<hold[temp.top().first]<<' ';
  43. temp.pop();
  44. }
  45. cout<<endl;
  46.  
  47. for(auto iter:hold){
  48. iter.second--;
  49. }
  50. }
  51.  
  52.  
  53. return ans;
  54. }
  55. };
Advertisement
Add Comment
Please, Sign In to add comment