-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path1834-single-threaded-cpu.py
36 lines (27 loc) · 1.1 KB
/
1834-single-threaded-cpu.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
# time complexity: O(nlogn)
# space complexity: O(n)
from heapq import heappop, heappush
from typing import List, Tuple
class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
nextTask: List[Tuple[int, int]] = []
result = []
sortedTasks = [(enqueueTime, processTime, idx)
for idx, (enqueueTime, processTime) in enumerate(tasks)]
sortedTasks.sort()
currTime = 0
taskIdx = 0
while taskIdx < len(tasks) or nextTask:
if not nextTask and currTime < sortedTasks[taskIdx][0]:
currTime = sortedTasks[taskIdx][0]
while taskIdx < len(sortedTasks) and currTime >= sortedTasks[taskIdx][0]:
_, processTime, originalIdx = sortedTasks[taskIdx]
heappush(nextTask, (processTime, originalIdx))
taskIdx += 1
processTime, index = heappop(nextTask)
currTime += processTime
result.append(index)
print(sortedTasks)
return result
tasks = [[1, 2], [2, 4], [3, 2], [4, 1]]
print(Solution().getOrder(tasks))