【力扣621. 任务调度器】桶思想python3

题目描述

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

思路题解

原来的思路,想着模拟一下,结果有测试用例死活过不了

class Solution:
    def leastInterval(self, tasks: List[str], n: int) -> int:
        if n==0:return len(tasks)
        m={}
        ans=0
        for s in tasks:
            if s in m:m[s]+=1
            else:m[s]=1
        flag=True
        maxi=max(m.values())
        m1=sorted(m.items(),reverse=True,key=lambda x:x[1])
        m.clear()
        for k,v in m1:
            m[k]=v
        print(m)
        while m:
            count=0
            for s in list(m.keys()):
                if count>=n+1: break
                m[s]-=1
                count+=1
                # print(m,s,count,ans)
                if m[s]==0:
                    del m[s]
            if m and ans==(maxi-1)*(n+1) and flag and max(m.values())<=n+1:
                print(max(m.values()))
                print("0")

                return len(tasks)
            if count<n+1 and m:
                ans+=n+1
                flag=False
                # print("daiming",n+1-count)
            else:
                ans+=count
                # print("count",count)
            count=0
        print("1")
        return ans

大佬题解:
https://leetcode-cn.com/problems/task-scheduler/solution/wan-zheng-de-qing-xi-de-jie-shi-yong-yi-ge-li-zi-s/
在这里插入图片描述

class Solution:
    def leastInterval(self, tasks, n):
        count = [0] * 26
        for ch in tasks:
            count[ord(ch) - ord('A')] += 1

        count_max = max(count)
        total = (count_max - 1) * (n + 1)

        for _ in count:
            if _ == count_max:
                total += 1

        return max(total, len(tasks))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值