Minimum Days to Execute Sequential Jobs with Cooldown
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Simulates executing a fixed sequence of jobs where re-running the same job requires waiting through a cooldown period.
You have a list of jobs that must be executed in the exact order given. Each job is identified by an integer ID and takes one day to complete. Only one job can run per day.
After completing a job with ID x on day d, you cannot run another job with the same ID x until at least cooldown days have passed. Specifically, the next time you can execute job x is on day d + cooldown + 1 or later.
If a job cannot be scheduled on the current day due to its cooldown restriction, that day becomes idle and no work is performed.
Return the minimum number of days required to complete all jobs in the given sequence.
Constraints
1 <= len(jobs) <= 2 * 10^50 <= jobs[i] <= 10^90 <= cooldown <= 10^5
Examples
Example 1
Input: min_days_to_complete_tasks([1, 2, 1, 3, 2, 1], 2)
Output: 7
Example 2
Input: min_days_to_complete_tasks([1, 1, 1], 3)
Output: 9
Example 3
Input: min_days_to_complete_tasks([1, 2, 3, 4], 100)
Output: 4
Hints
Hint 1
Track the most recent execution day for each job ID using a dictionary.
Hint 2
For each job, determine the earliest day it can run based on both the cooldown constraint and the requirement to run after the previous job.
Hint 3
The answer is simply the day on which the final job completes—idle days are implicitly counted in the gaps.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Since jobs must execute in a fixed order, we process them sequentially and schedule each job on the earliest valid day. For each job, we determine two constraints:
- Sequential constraint: The job must run strictly after the previous job (at least
previous_day + 1) - Cooldown constraint: If this job ID was previously executed on day
d, it cannot run before dayd + cooldown + 1
We take the maximum of these two bounds to find the actual execution day. A hash map tracks the most recent execution day for each job ID—we only need the latest occurrence since earlier runs impose weaker constraints.
The final job's execution day gives the total calendar days needed, with idle periods implicitly captured by the gaps created when cooldown constraints push jobs into the future.
Why This Works
The greedy "schedule as early as possible" strategy is optimal because constraints are monotone: scheduling a job earlier never forces later jobs to be delayed. Each job is placed on the earliest day that satisfies all applicable constraints, which is provably a lower bound for any valid schedule. Since our schedule is also valid (never violates sequencing or cooldown rules), it achieves the minimum.
Complexity
- Time: O(n) where n is the number of jobs. We make one pass through the list with O(1) average-case hash map operations per job.
- Space: O(u) where u is the number of distinct job IDs, which is at most O(n).
Note: Day numbers can reach approximately n × (cooldown + 1) ≈ 2×10^10, so 64-bit integers are needed in languages like C++ or Java.
Solution
from typing import List
def min_days_to_complete_tasks(jobs: List[int], cooldown: int) -> int:
last_run = {}
current_day = 0
for job_id in jobs:
if job_id in last_run:
earliest_allowed = last_run[job_id] + cooldown + 1
else:
earliest_allowed = 1
current_day = max(current_day + 1, earliest_allowed)
last_run[job_id] = current_day
return current_day