← 返回 scale.ai 的题目列表Task Scheduler with Dependencies (Add/Consume)
类型:online_judge
Problem: Task Scheduler with Dependencies (Part 2)
Extend Part 1: tasks may depend on a list of subtasks. You still implement:
addTasks(tasks)
consumeTask()
Task format
Each task includes:
taskId
deadline (smaller = higher priority)
subtasks: list of dependent taskIds
Dependency rules
A task becomes consumable only after all its subtasks have been consumed.
consumeTask() must return the consumable task with the smallest deadline (tie-break as in Part 1).
Consuming a task marks it done and may unlock its parent tasks.
If no task is consumable, return None/null.
Notes
Tasks can be added in any order; a parent may be added before its subtasks.
Maintain parent/child relationships so that when the last subtask completes, the parent becomes consumable.
Constraints
Total tasks N up to 2e5
Total dependency edges E up to 2e5
Sample tests
[(1,5,[]),(2,3,[1])] => consumes: 1,2
[(1,5,[]),(2,4,[]),(3,10,[1,2])] => 2,1,3
Add parent first: [(3,1,[1,2])] then [(1,5,[]),(2,4,[])] => 2,1,3
Only blocked tasks => consume returns None
One subtask unlocks multiple parents => 1,2,3
Example
Input
addTasks: [(1,5,[]),(2,3,[1])]
consumeTask(); consumeTask()
Output
1 2