File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ class  Solution  {
2+     public  int  leastInterval (char [] tasks , int  n ) {
3+         if  (n ==0 ) return  tasks .length ;
4+         PriorityQueue <Integer > pq  = new  PriorityQueue <>((a ,b )->b -a );
5+         Queue <Pair <Integer , Integer >> q  = new  LinkedList <>();
6+         int [] arr  = new  int [26 ];
7+         for  (char  c : tasks ) 
8+             arr [c -'A' ]++;
9+         for  (int  val : arr ) 
10+             if  (val >0 ) 
11+                 pq .add (val );
12+         int  time  = 0 ;
13+         // System.out.println(pq); 
14+         // System.out.println(q); 
15+         while  ((!pq .isEmpty () || !q .isEmpty ())) {
16+             if  (pq .isEmpty ()) {
17+                 time  = Math .max (q .peek ().getValue (), time );
18+                 pq .add (q .poll ().getKey ());
19+             }
20+             int  val  = pq .poll ();
21+             val --;
22+             time ++;
23+             if  (val >0 ) q .add (new  Pair (val , time +n ));
24+ //             System.out.println(q + " "+ time); 
25+         }
26+         return  time ;
27+     }
28+ }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments