- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathday20_621.py
31 lines (27 loc) · 1.47 KB
/
day20_621.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fromtypingimportCounter, List
classSolution:
defleastInterval(self, tasks: List[str], n: int) ->int:
"""
case1: AAABB => AB*AB*A (max_count - 1) * (n+1) + 1
case2: AAABBB => AB*AB*AB (max_count - 1) * (n+1) + 2
case3: AAAAAAAGH => AG*AH*A*A*A*A (max_count - 1) * (n+1) + 1
case4: AAAAAGHBBBB => ABG*ABH*AB*AB (max_count - 1) * (n+1) + 2
case5: AABCDEFGH => AB*ACDEFGH (max_count - 1) * (n+1) + 1 ? (2-1)* (1 ..= 7) + 1 < len(case4)=9
"""
iflen(tasks) <1:
raiseKeyError
ifn==0:
returnlen(tasks)
counter=Counter(tasks)
_, most_count=counter.most_common(1)[0]
returnmax(
len(tasks), (most_count-1) * (n+1) +sum(1ifmost_count==valueelse0forvalueincounter.values())
)
if__name__=="__main__":
assertSolution().leastInterval(tasks=["A", "A", "A", "B", "B", "B"], n=2) ==8
assertSolution().leastInterval(tasks=["A", "A", "A", "B", "B", "B"], n=0) ==6
assertSolution().leastInterval(tasks=["A", "A", "A", "A", "A", "A", "B", "C", "D", "E", "F", "G"], n=2) ==16
assertSolution().leastInterval(tasks=["A"], n=2) ==1
assertSolution().leastInterval(tasks=["A", "A"], n=2) ==4
assertSolution().leastInterval(tasks=["A", "A", "B", "C"], n=1) ==4
assertSolution().leastInterval(tasks=["A", "A", "A", "B", "B", "B"], n=1) ==6