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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
| # {
# 'A': 3,
# 'B': 3,
# 'C': 3,
# 'D': 2,
# 'E': 1
# }
# n = 3
# [
# ['A', 'B', 'C', 'D'],
# ['A', 'B', 'C', 'D'],
# ['A', 'B', 'C', 'E'],
# ]
########################################
# {
# 'A': 3,
# 'B': 3,
# 'C': 3,
# 'D': 2,
# 'E': 2
# }
# n = 3
# [
# ['A', 'B', 'C', 'D'],
# ['A', 'B', 'C', 'D'],
# ['A', 'B', 'C', 'E'],
# ['E'],
# ]
########################################
# {
# 'A': 3,
# 'B': 3,
# 'C': 3,
# 'D': 2,
# 'E': 1
# }
# n = 2
# [
# ['A', 'B', 'C']
# ['A', 'B', 'D']
# ['A', 'B', 'C']
# ['D', 'E', 'C']
# ]
from collections import defaultdict, deque
import heapq
class Solution:
def leastInterval(self, tasks, n: int) -> int:
if n == 0:
return len(tasks)
n_tsk = defaultdict(int)
for tsk in tasks:
n_tsk[tsk] -= 1
heap_queu = [v for v in n_tsk.values()]
# [-1, -3, -2]
heapq.heapify(heap_queu)
# [-3, -2, -1]
output = 0
while heap_queu:
i = 0
for _ in range(n+1):
if not heap_queu:
return output
if i == len(heap_queu):
output += 1
continue
output += 1
heap_queu[i] += 1
if heap_queu[i] == 0:
heap_queu.pop(i)
continue
i += 1
return output
|