Task Scheduler


  • 0

    Click here to see the full article post


  • 0
    J

    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
    Z

    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()
        lst.sort(reverse=True)
        max_val = lst[0] - 1
        print(max_val)
        res = max_val*n
        for num in lst[1:]:
            if num >= max_val:
                res -= max_val
            else:
                res -= num
        if res < 0:
            return len(tasks)
        else:
            return res + len(tasks)

  • 0

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


  • 0
    D

    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
    A

    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.