Untitled

 avatar
unknown
python
5 months ago
1.5 kB
6
Indexable
class Solution:
    def leastInterval(self, tasks: List[str], n: int) -> int:
        next_index = {key: 0 for key in tasks}
        task_count = {}
        for task in tasks:
            task_count[task] = task_count.get(task, 0) + 1
        
        more_than_1 = False
        for task in tasks:
            if task_count[task] > 1:
                more_than_1 = True
        if not more_than_1:
            return len(task) + 1

        sorted_tasks = []
        tasks_keys_sorted = sorted(task_count, key=task_count.get, reverse=True)
        for task in tasks_keys_sorted:
            sorted_tasks.extend([task] * task_count[task])

        tasks = sorted_tasks
        print(sorted_tasks)
        # print(task_count)

        current_index = 0

        for task in tasks:
            print(current_index)
            print(task)
            if task_count[task] == 1 and next_index[task] <= current_index:
                current_index += 1

            if task_count[task] > 1:
                if next_index[task] is 0:
                    next_index[task] = (current_index + n + 1)
                    current_index += 1
                else:
                    next_index[task] = next_index[task] + n + 1
                print(next_index[task])
                task_count[task] -= 1
            
            
            print("\n")
        
        print(next_index)

        return next_index[max(next_index, key=next_index.get)] + 1
        
        
        
Editor is loading...
Leave a Comment