chenye-814 / DTSTRCT-ALGRTHM

0 stars 0 forks source link

Dec-30 Single-Threaded CPU #5

Open chenye-814 opened 1 year ago

chenye-814 commented 1 year ago

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

chenye-814 commented 1 year ago
view code ```py from heapq import heappush, heappop class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: ans = [] heap = [] serial_tasks = sorted((task[0], task[1], idx) for idx, task in enumerate(tasks)) i, n, curr_time = 0, len(tasks), serial_tasks[0][0] while i < n or heap: while i < n and (task := serial_tasks[i])[0] <= curr_time : heappush(heap, task[1:]) i += 1 if heap: process_time, index = heappop(heap) ans.append(index) curr_time += process_time else: curr_time = task[0] return ans ```