Skip to content

Commit e02f0bf

Browse files
authored
Create single-threaded-cpu.py
1 parent 215ef8e commit e02f0bf

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python/single-threaded-cpu.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
import heapq
5+
6+
7+
class Solution(object):
8+
def getOrder(self, tasks):
9+
"""
10+
:type tasks: List[List[int]]
11+
:rtype: List[int]
12+
"""
13+
idx = range(len(tasks))
14+
idx.sort(key=lambda x: tasks[x][0])
15+
result, min_heap = [], []
16+
i, time = 0, tasks[idx[0]][0]
17+
while i < len(idx) or min_heap:
18+
while i < len(idx) and tasks[idx[i]][0] <= time:
19+
heapq.heappush(min_heap, (tasks[idx[i]][1], idx[i]))
20+
i += 1
21+
if not min_heap:
22+
time = tasks[idx[i]][0]
23+
continue
24+
t, j = heapq.heappop(min_heap)
25+
time += t
26+
result.append(j)
27+
return result

0 commit comments

Comments
 (0)