Problem · Array
Minimum Days for Fixed-Order Tasks with Cooldown
Learn this problemProblem statement
You are given an array tasks of string task IDs and a nonnegative integer cooldown. Execute every task exactly once, in the supplied order, with at most one task executed per day.
If the same task ID was last executed on day d, its next execution must be no earlier than day d + cooldown + 1. You may insert idle days, but you may not reorder tasks.
Return the minimum total number of days needed to execute the complete sequence. Return 0 when tasks is empty.
Function
minimumDays(tasks: String[], cooldown: int) → longExamples
Example 1
tasks = ["A","B","A"]cooldown = 2return = 4Execute A on day 1 and B on day 2. Day 3 must be idle, so the second A runs on day 4.
Example 2
tasks = ["x","x","x"]cooldown = 0return = 3With a zero-day cooldown, equal tasks may run on consecutive days.
Example 3
tasks = []cooldown = 5return = 0No execution days are needed for an empty sequence.
Constraints
0 <= tasks.length <= 100000.- Each task ID is a non-empty string containing at most 50 Unicode characters.
0 <= cooldown <= 1000000000.- Tasks must be executed in their given order.
- The answer fits in a signed 64-bit integer.