Leetcode 1962 Solution

This article provides solution to leetcode question 1962 (single-threaded-cpu)

https://leetcode.com/problems/single-threaded-cpu

Solution

class Solution:
    def getOrder(self, tasks: List[List[int]]) -> List[int]:
        sorted_tasks = []
        for i, task in enumerate(tasks):
            sorted_tasks.append((task[0], task[1], i))
        sorted_tasks.sort()

        q = []

        i = 0
        t = 0
        ans = []
        while q or i < len(sorted_tasks):
            while i < len(sorted_tasks) and sorted_tasks[i][0] <= t:
                heapq.heappush(q, (sorted_tasks[i][1], sorted_tasks[i][2], sorted_tasks[i][0]))
                i += 1

            if q:
                processing_time, j, starting_time = heapq.heappop(q)
                t += processing_time
                ans.append(j)
            else:
                t = sorted_tasks[i][0]

        return ans