← 返回 scale.ai 的题目列表Task Scheduler with Dependencies (Add/Consume)
类型:online_judge
Problem: Task Scheduler (Part 1)
Implement a task scheduler that supports batch insertion and consuming tasks in priority order.
You must implement two methods:
addTasks(tasks): add a batch of tasks
consumeTask(): consume (pop and mark done) a single task
Task format
Each task is a pair:
taskId: unique identifier
deadline: integer deadline (smaller means more urgent)
Rules
The scheduler maintains a set of unfinished tasks.
Each call to consumeTask() must return the unfinished task with the smallest deadline.
If multiple tasks share the same deadline, break ties by taskId (e.g., smaller taskId first; if not specified, you may define and state the rule).
If there is no task to consume, return an empty value (None/null).
Suggested I/O
addTasks(tasks) takes a list: [(taskId, deadline), ...]
consumeTask() returns a taskId (or the full record, but be consistent)
Constraints
Up to 2e5 tasks per batch
32-bit integer deadlines
Sample tests
Basic ordering: add [(1,5),(2,3),(3,7)] => consume => 2
Multiple consumes => 2,1,3
Empty scheduler => None
Tie-breaking: add [(10,1),(2,1),(7,2)] => consume => 2 (if smaller id first)
Multiple batches: add (1,4), then (2,2),(3,3) => consumes => 2,3,1
Example
Input
addTasks: [(1,5),(2,3),(3,7)]
consumeTask()
Output
2