Skip to content

LC 1834 [M] Single Threaded CPU

Code with Senpai edited this page Mar 9, 2022 · 1 revision

min heap and fast forwarding of time

class Solution:
    def getOrder(self, tasks: List[List[int]]) -> List[int]:
        for i, t in enumerate(tasks):
            t.append(i)
            
        tasks.sort(key = lambda t: t[0])
        
        res = []
        minHeap = []
        i = 0
        time = tasks[0][0]
        
        while minHeap or i < len(tasks):
            while i < len(tasks) and time >= tasks[i][0]: 
                heapq.heappush(minHeap, [tasks[i][1], tasks[i][2]])
                i += 1
            
            if not minHeap:
                time = tasks[i][0]
            else:
                procTime, index = heapq.heappop(minHeap)
                time += procTime
                res.append(index)
        return res
Clone this wiki locally