Leetcode 621 Solution

This article provides solution to leetcode question 621 (task-scheduler)

https://leetcode.com/problems/task-scheduler

Solution

class Solution:
    def leastInterval(self, tasks: List[str], n: int) -> int:
        m = collections.defaultdict(int)
        for task in tasks:
            m[task] += 1

        a = sorted([cnt for cnt in m.values()], reverse=True)
        if not a:
            return 0

        limit = a[0] - 1
        free_cells = limit * (n + 1)

        for cnt in a:
            free_cells -= min(limit, cnt)
            if free_cells < 0:
                return len(tasks)

        return len(tasks) + free_cells