mycode   53.01%

这个题在纸上画一画就知道啦,只要出现次数最多的字母能够满足要求,其他更少的字母穿插在其中,间隔就更满足<n啦,当然,最后不要忘记加上尾巴哦,尾巴和出现次数最多的字母的种类有关哦!

class Solution(object):
    def leastInterval(self, tasks, n):
        """
        :type tasks: List[str]
        :type n: int
        :rtype: int
        """
        from collections import Counter
        count = Counter(tasks)
        total = 1
        maxcount = count.most_common()[0][1]
        print(maxcount)
        for num,c in count.most_common()[1:]:
            if c == maxcount:
                total += 1
        return max(len(tasks),(maxcount-1)*(n+1)+total)

参考:

class Solution(object):
    def leastInterval(self, tasks, n):
        """
        :type tasks: List[str]
        :type n: int
        :rtype: int
        """
        lenT = len(tasks)
        res = rem = max_cnt = 0
        dic = collections.defaultdict(int)
       
        for ch in tasks:
            dic[ch]+=1
        for val in dic.values():
            if val>max_cnt:
                max_cnt = val
                rem = 0
            elif val==max_cnt:
                rem+=1 #与max_cnt出现频率相同的其他元素个数
        tmp = (max_cnt-1)*(n+1)+1 + rem # n很长的情况
        res = max(lenT, tmp)
        return res