Skip to content

Commit

Permalink
Create 1834. Single-Threaded CPU
Browse files Browse the repository at this point in the history
Signed-off-by: Tahsin Tunan <[email protected]>
  • Loading branch information
tahsintunan committed Dec 30, 2022
1 parent 2d0e0cc commit b6f3a4f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions python/1834-Single-Threaded-CPU.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
tasks = sorted([(t[0], t[1], i) for i, t in enumerate(tasks)])
result, heap = [], []
cur_task_index = 0
cur_time = tasks[0][0]

while len(result) < len(tasks):
while (cur_task_index < len(tasks)) and (tasks[cur_task_index][0] <= cur_time):
heapq.heappush(heap, (tasks[cur_task_index][1], tasks[cur_task_index][2]))
cur_task_index += 1
if heap:
time_difference, original_index = heapq.heappop(heap)
cur_time += time_difference
result.append(original_index)
elif cur_task_index < len(tasks):
cur_time = tasks[cur_task_index][0]

return result

0 comments on commit b6f3a4f

Please sign in to comment.