Category-Dependent Task Scheduling with Cooldown
You are given a list of tasks, where each task is represented as [start, end, category].
startis the start time of the task.endis the end time of the task.categoryis an integer representing the category of the task.
The duration of a task is end - start.
You want to select a set of non-overlapping tasks to run on a single machine such that the sum of their durations is maximized.
Two selected tasks $A$ and $B$ (where task $A$ finishes before task $B$ starts) can be scheduled sequentially if and only if:
- Task $A$ ends at or before task $B$ starts ($end_A \le start_B$).
- If their categories are different ($category_A \neq category_B$), a cleanup/cooldown time of length
cooldownis required between them ($end_A + cooldown \le start_B$). - If their categories are the same ($category_A = category_B$), no cooldown is required ($end_A \le start_B$).
Return the maximum total task duration that can be scheduled.
[ "[[1, 5, 1], [5, 8, 1], [6, 10, 2]]", "2" ]
Explanation. Selecting Task 0 ([1, 5, 1], duration 4) and Task 1 ([5, 8, 1], duration 3) yields total duration 7. Both tasks share category 1, so no cooldown is needed between them (5 <= 5). Task 2 cannot be scheduled after Task 0 because 5 + 2 = 7 > 6.
[ "[[1, 4, 1], [6, 10, 2], [10, 15, 2]]", "2" ]
Explanation. All three tasks can be scheduled sequentially. Task 0 ends at 4, and since Task 1 has a different category, cooldown 2 allows Task 1 to start at 6. Task 1 and Task 2 have the same category, so Task 2 can start immediately at 10. Total duration is 3 + 4 + 5 = 12.
[ "[[0, 10, 1], [2, 5, 2], [7, 9, 3]]", "3" ]
Explanation. Taking Task 0 alone gives duration 10. Tasks 1 and 2 cannot be paired because Task 1 ends at 5 and requires cooldown 3 to switch categories, meaning the next task could only start at 8, but Task 2 starts at 7.
[ "[[1, 3, 5]]", "10" ]
Explanation. Only one task is available, giving a total duration of 2.
[ "[[1, 3, 1], [3, 5, 2], [5, 7, 1]]", "0" ]
Explanation. With a cooldown of 0, tasks can be switched seamlessly regardless of category. Scheduling all three tasks gives 2 + 2 + 2 = 6.
[ "[[1, 5, 1], [2, 4, 1], [4, 6, 1]]", "5" ]
Explanation. Task 1 ([2, 4, 1], duration 2) and Task 2 ([4, 6, 1], duration 2) can be combined for a total duration of 4, matching Task 0 ([1, 5, 1], duration 4). The maximum duration achievable is 4.
Follow-up: What if each category $C$ requires its own distinct cooldown duration $cooldown[C]$ when switching away to a different category?
- `1 <= tasks.length <= 10^5` - `tasks[i]` contains exactly 3 integers: `[start, end, category]` - `-10^9 <= start < end <= 10^9` - `1 <= category <= 10^9` - `0 <= cooldown <= 10^9`