← 返回 scale.ai 的题目列表Task Processor with Dependencies (Subtasks Must Be Consumed First)
类型:online_judge
Extend the Task Processor with a dependency field subtasks.
Each task has:
id: unique id
deadline: smaller means earlier
subtasks: list of task ids that must be consumed before this task becomes available
Implement:
AddTask(task)
ConsumeTask() which consumes, among currently available tasks (all subtasks already consumed), the one with the smallest deadline and returns its id.
Example:
task1 = {"id":1,"deadline":2,"subtasks":[2]}
task2 = {"id":2,"deadline":4,"subtasks":[]} Consumption order should be: 2 then 1.
Constraints:
Up to ~1e5 tasks and many dependencies; aim for an efficient design.
Example
Input
Add: task1={"id":1,"deadline":2,"subtasks":[2]}
Add: task2={"id":2,"deadline":4,"subtasks":[]}
Consume x2
Output
2
1