File tree Expand file tree Collapse file tree 1 file changed +34
-1
lines changed Expand file tree Collapse file tree 1 file changed +34
-1
lines changed Original file line number Diff line number Diff line change 88    Time: O(n) 
99    Space: O(1) 
1010*/ 
11- 
11+ /* 
1212class Solution { 
1313public: 
1414    int leastInterval(vector<char>& tasks, int n) { 
@@ -38,3 +38,36 @@ class Solution {
3838        return tasks.size() + idles; 
3939    } 
4040}; 
41+ */ 
42+ 
43+ //  Time O(n * cooldown)
44+ class  Solution  {
45+ public: 
46+     int  leastInterval (vector<char >& tasks, int  n) {
47+         priority_queue<int > pq;
48+         queue<vector<int >> q;
49+         vector<int > counter (26 );
50+ 
51+         for  (int  i = 0 ; i < tasks.size (); ++i)
52+             ++counter[tasks[i] - ' A' 
53+         for  (int  i = 0 ; i < 26 ; ++i){
54+             if  (counter[i])
55+                 pq.push (counter[i]);
56+         }
57+ 
58+         int  time = 0 ;
59+         while  (!q.empty () || !pq.empty ()){
60+             ++time;
61+             if  (!pq.empty ()){
62+                 if  (pq.top () - 1 )
63+                     q.push ({pq.top () - 1 , time + n});
64+                 pq.pop ();
65+             }
66+             if  (!q.empty () && q.front ()[1 ] == time){
67+                 pq.push (q.front ()[0 ]);
68+                 q.pop ();
69+             }
70+         }
71+         return  time;
72+     }
73+ };
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments