-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask_scheduler.py
More file actions
55 lines (46 loc) · 1.72 KB
/
task_scheduler.py
File metadata and controls
55 lines (46 loc) · 1.72 KB
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
"""
Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task.
Tasks could be done in any order. Each task is done in one unit of time.
For each unit of time, the CPU could complete either one task or just be idle.
However, there is a non-negative integer n that represents the cooldown period between two same tasks
(the same letter in the array), that is that there must be at least n units of time between any two same tasks.
Return the least number of units of times that the CPU will take to finish all the given tasks.
"""
import heapq
def task_scheduler(tasks, n):
if n == 0:
return len(tasks)
time = 0
# count frequency
freq = [0]*26
for task in tasks:
freq[ord(task)-ord('A')] += 1
# create priority queue for tasks
pq = []
heapq.heapify(pq)
for f in freq:
if f != 0:
heapq.heappush(pq, -1*f)
while len(pq) > 0:
print(pq)
remainingTasks = []
cycle = n+1
while cycle > 0 and len(pq) > 0:
taskFreq = -1*heapq.heappop(pq)
taskFreq -= 1 # accomplish task
if taskFreq > 0:
remainingTasks.append(-1*taskFreq)
cycle -= 1 # idle time is utilized against task
time += 1 # time required to execute this task
for remainingTask in remainingTasks:
heapq.heappush(pq, remainingTask)
if len(pq) > 0:
time += cycle
return time
if __name__ == "__main__":
tasks = ["A", "A", "A", "B", "B", "B"]
n = 2
print(task_scheduler(tasks, n))
tasks = ["A", "A", "A", "A", "A", "A", "B", "C", "D", "E", "F", "G"]
n = 2
print(task_scheduler(tasks, n))