1
class Solution:
2
def getOrder(self, tasks: List[List[int]]) -> List[int]:
3
# For better readability
4
Task = namedtuple("Task", ["etime", "ptime", "index"])
5

6
# Sort the tasks by enqueue time, shortest processing time and index
7
stasks = sorted([Task(task[0], task[1], i) for i, task in enumerate(tasks)])
8
# t: current CPU clock; i: current task index
9
t = i = 0
10
heap, result = [], []
11

12
while len(result) < len(stasks):
13
# Push all the tasks available at current CPU clock
14
while i < len(stasks) and stasks[i].etime <= t:
15
heappush(heap, (stasks[i].ptime, stasks[i].index))
16
i += 1
17
if heap:
18
ptime, index = heappop(heap)
19
result.append(index)
20
t += ptime
21
else:
22
# Jump to the next available task
23
t = stasks[i].etime
24
return result

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0