-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskScheduler.py
70 lines (59 loc) · 1.82 KB
/
taskScheduler.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
62
63
64
65
66
67
68
69
70
#621. Task Scheduler
# Input: tasks = ["A","A","A","B","B","B"], n = 2
# Output: 8
# Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
# You need to return the least number of intervals the CPU will
# take to finish all the given tasks.
# AAABBBCCD, n = 3
# A???A???A
# AB??AB??AB
def taskScheduler1(tasks, n):
'''
input: list[char]
output: int
'''
counter = [0] * 26
for task in tasks:
counter[ord(task) - ord('A')] += 1
maxCount = max(counter)
numOfMaxCount = 0
for count in counter:
if count == maxCount:
numOfMaxCount += 1
partCount = maxCount - 1
emptySpot = partCount * (n - numOfMaxCount + 1)
availableTask = len(tasks) - numOfMaxCount * maxCount
idle = max(0, emptySpot - availableTask)
# ["A","A","A","B","B","B"], 0 in this case, idle would be negative
ans = len(tasks) + idle
return ans
print(taskScheduler1(["A","A","A","B","B","B","C","C","D"], 3))
print(taskScheduler1(["A","A","A","B","B","B"], 2))
import heapq
import collections
def taskScheduler2(tasks, n):
counter = collections.Counter(tasks)
queue = []
heapq.heapify(queue)
for char, count in counter.items():
heapq.heappush(queue, (-1 * count, char))
#[(-3, A), (-3, B), (-2, C), (-1, D)]
ans = 0
while queue:
#[(-1, A), (-1, B)]
k = n + 1
temp = []
while k > 0 and queue:
count, char = heapq.heappop(queue)
temp.append((-1 * count, char))
k -= 1
ans += len(temp)
for count, key in temp:
if count - 1 > 0:
heapq.heappush(queue, (-1*(count-1), char))
if len(queue) == 0:
break
if k > 0:
ans += k
return ans
print(taskScheduler2(["A","A","A","B","B","B","C","C","D"], 3))