Constant Time
Constant Time
/Heaps
Heaps
/Similar Questions : Greedy
Similar Questions : Greedy
/
Solution : Task Scheduler

Solution : Task Scheduler

class Solution:
    def leastInterval(self, tasks: List[str], n: int) -> int:
        
        
        
        cooldown = collections.deque() # [count, item , time]
        
        
        temp  = Counter(tasks).items()
        priority = [[-1*v,k] for k,v in temp]
        heapq.heapify(priority)
        
        
        
        time = 0
        
        while priority or cooldown:
            
            if priority:
                count,task  = heapq.heappop(priority)
                cooldownMoment = time + n
                if count+1: # non zero
                    cooldown.append([count+1,task,cooldownMoment])
                    
            if cooldown:
                cooldownMomentReached = cooldown[0][2] == time
                if cooldownMomentReached:
                    count,task,time = cooldown.popleft()
                    heapq.heappush(priority,[count,task])
        
        
            time+=1
        
        
        
        return time
Logo
YouTubeDiscord