← 返回 scale.ai 的题目列表Task Processor: AddTask and ConsumeTask (Earliest Deadline First)
类型:online_judge
Implement a Task Processor that supports consuming tasks by earliest deadline first.
Each task has:
id: unique task identifier (integer)
deadline: integer deadline; smaller means more urgent
Implement two functions:
AddTask(task): add a task into the processor.
ConsumeTask(): pick and remove one currently available task with the smallest deadline, and return its id.
Notes:
Each ConsumeTask() consumes at most one task.
Tasks can be added dynamically and ConsumeTask() can be called many times.
Example:
Add tasks [{"id":1,"deadline":2}, {"id":2,"deadline":4}]
Calling ConsumeTask() twice should return 1, then 2.
Constraints:
Up to around 1e5 tasks; design for efficient repeated Add/Consume.
Example
Input
Add: [{"id":1,"deadline":2}]
Consume x1
Output
1