Task Scheduler

  • 0

    Click here to see the full article post

  • 0

    In the approach 1, I think instead of

    if (map[25] == 0 && map[25 - i] == 0)

    This would suffice

    if (map[25] == 0)

  • 0

    @JkApacc Thanks for your suggestion. I have updated the code.

  • 0

    Here is my simple Python solution, and costs 669 ms, beats 100%
    What's more, it can be modified to be faster, because the sort is not necessary, and we just need to get the top two most frequent tasks.
    The idea is quite simple. So I don't explain it here.
    The code is as follows:

    class Solution(object):
    def leastInterval(self, tasks, n):
    :type tasks: List[str]
    :type n: int
    :rtype: int

        dct = {}
        for task in tasks:
            dct[task] = dct.get(task, 0) + 1
        lst = dct.values()
        max_val = lst[0] - 1
        res = max_val*n
        for num in lst[1:]:
            if num >= max_val:
                res -= max_val
                res -= num
        if res < 0:
            return len(tasks)
            return res + len(tasks)

  • 0

    @zhanzq Nice solution. Thanks for sharing it. I have added your approach in the article. Thanks.

  • 0

    sort make it easier to understand. But actually all we need is to find out the biggest number in the map, just a linear scan would be sufficient.

  • 0

    i think PriorityQueue solution is just as same as Sort solution since we insert element one by one into the queue, it's just a heap-sort..

Log in to reply

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