-
Notifications
You must be signed in to change notification settings - Fork 2
/
q0621.py
61 lines (57 loc) · 1.71 KB
/
q0621.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
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
#!/usr/bin/python3
from typing import List
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
dic = {}
for str in tasks:
if str not in dic:
dic[str] = 0
dic[str] += 1
result = 0
values = list(dic.values())
values.sort()
maxVal = values[len(values) - 1]
length = (maxVal - 1) * (n + 1)
index = len(values) - 1
while index >= 0 and values[index] == maxVal:
length += 1
index -= 1
if length > len(tasks):
return length
else:
return len(tasks)
def leastInterval2(self, tasks: List[str], n: int) -> int:
dic = {}
for str in tasks:
if str not in dic:
dic[str] = 0
dic[str] += 1
result = 0
values = list(dic.values())
values.sort()
lastIdle = 0
while len(values) > 0:
length = len(values)
if length > n:
result += n + 1
index = length - 1
while index >= length - 1 - n:
values[index] -= 1
if values[index] == 0:
values.pop(index)
index -= 1
values.sort()
else:
result += n + 1
lastIdle = n + 1 - length
for i in reversed(range(length)):
values[i] -= 1
if values[i] == 0:
values.pop(i)
values.sort()
return result - lastIdle
solu = Solution()
tasks = ["A","A","A","B","B","B"]
n = 2
result = solu.leastInterval(tasks, n)
print(result)