My Python Solution


  • 0
    T

    '''
    class Solution(object):
    def leastInterval(self, tasks, n):

        :type tasks: List[str]
        :type n: int
        :rtype: int
    
    
        # Make a table to count how many of each tasks you have 
        table = {} 
        for task in tasks:
            table[task] = table.get(task, 0) + 1
        
        # Keeps track of how many idles we need to insert in our process
        idles = 0 
        
        # While there are still elements in the table 
        while table:
            length = len(table)           # important to keep track of the length of the table before 
                                                       deleting elements
            for key in table.keys():      # iterate through the dictionary and subtract each tasks by 1 
                                                        and delete the tasks if all of it is  completed
                table[key] -= 1
                if table[key] == 0:       
                    del table[key]
            if len(table) == 0:           # necessary if statement b/c if there are no elements left, there is no need to calc idles                                                     proceeding it
                break
            idles += (n - length + 1)     # The addition of 1 is needed to offset the addition of the element 
        
        if idles < 0:                     # A negative idle count means idles were not needed
            return len(tasks)
        else:
            return idles + len(tasks) 
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.