LeetCode 1834. [Python] Single-Threaded CPU
原题链接
中等
作者:
徐辰潇
,
2021-04-20 02:59:43
,
所有人可见
,
阅读 554
class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
#TC: O(n log n)
#SC: O(n)
#tasks.sort(key = lambda task: task[0])
tasks = sorted([[task[0], task[1], idx] for idx, task in enumerate(tasks)])
t = tasks[0][0]
totalTasks = len(tasks)
finishedTasks = 0
p = 0
Heap = []
res = []
while finishedTasks < totalTasks:
while p < totalTasks and tasks[p][0] <= t:
heapq.heappush(Heap, (tasks[p][1], tasks[p][2], tasks[p][0]))
p+=1
if len(Heap) > 0:
nextTask = heapq.heappop(Heap)
res.append(nextTask[1])
finishedTasks += 1
t += nextTask[0]
else:
t = tasks[p][0]
return res