题目描述
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
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))