← 返回 waymo 的题目列表Task Scheduling with Deadline
类型:online_judge
Given a list of tasks, each with a specified duration and a list of prerequisite tasks that must be completed before it. There is also a global deadline by which all tasks need to be completed. Determine an optimal order to schedule the tasks so that all can be completed within the given deadline.
Specifications:
Each task has the following properties:
id: Unique identifier for the task.
duration: Time in hours required to complete the task.
prerequisites: Array of tasks that must be completed before this task.
Input format:
An integer n indicating the number of tasks.
An array tasks where each item is a dictionary with id, duration, and prerequisites.
An integer deadline indicating the total time by which all tasks must be completed.
Output format:
If it's possible to complete all tasks within the deadline, output the optimal scheduling order of task IDs ensuring prerequisites are respected.
If it's impossible to complete all tasks by the deadline, output -1.
Example input:
5
[
{"id": 1, "duration": 3, "prerequisites": []},
{"id": 2, "duration": 2, "prerequisites": [1]},
{"id": 3, "duration": 1, "prerequisites": []},
{"id": 4, "duration": 2, "prerequisites": [2, 3]},
{"id": 5, "duration": 4, "prerequisites": [4]}
]
10
Example output:
[1, 3, 2, 4, 5]
Constraints:
The number of tasks n is between 1 and 100.
Both duration and deadline are between 1 and 100.
Example
Input
5
[{"id": 1, "duration": 3, "prerequisites": []}, {"id": 2, "duration": 2, "prerequisites": [1]}, {"id": 3, "duration": 1, "prerequisites": []}, {"id": 4, "duration": 2, "prerequisites": [2, 3]}, {"id": 5, "duration": 4, "prerequisites": [4]}]
10