-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0621-task-scheduler.py
More file actions
39 lines (31 loc) · 817 Bytes
/
0621-task-scheduler.py
File metadata and controls
39 lines (31 loc) · 817 Bytes
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
# space complexity: O(n)
# time complexity: O(1)
from collections import Counter
from typing import List
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
taskCounter = Counter(tasks)
maxValue = max(taskCounter.values())
maxValueCount = list(taskCounter.values()).count(maxValue)
return max(len(tasks), (maxValue - 1) * (n + 1) + maxValueCount)
'''
A: 3
B: 3
A B i A B i A B
A: 3
B: 1
A B i A i i A
A: 3
B: 3
n = 3
A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.
'''
tasks = ["A", "A", "A", "B", "B", "B"]
n = 2
print(Solution().leastInterval(tasks, n))
tasks = ["A", "C", "A", "B", "D", "B"]
n = 1
print(Solution().leastInterval(tasks, n))
tasks = ["A", "A", "A", "B", "B", "B"]
n = 3
print(Solution().leastInterval(tasks, n))