class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
tasks = [(task[0], i, task[1]) for i, task in enumerate(tasks)]
tasks.sort()
backlog = []
time = 0
ans = []
pos = 0
for _ in tasks:
if not backlog:
time = max(time, tasks[pos][0])
while pos < len(tasks) and tasks[pos][0] <= time:
heapq.heappush(backlog, (tasks[pos][2], tasks[pos][1]))
pos += 1
d, j = heapq.heappop(backlog)
time += d
ans.append(j)
return ans
复杂度分析
令 n 为数组长度。
时间复杂度:$O(nlogn)$
空间复杂度:$O(n)$
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。