Show problems as
Classic problem #2365MediumArrayHash TableSimulation

Task Scheduler II

Real-world version: How Many Days the Run of Jobs Takes
Tracked in this browser only
Read Task Scheduler II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A run of jobs reads jobs, taken strictly in the order given, and each entry names the job's kind. Days are counted from one.

On any day you may either finish the next job in the run or take the day off, but a job may only be finished when at least gap whole days have passed since the last job of the same kind was finished.

Return the fewest days the whole run takes, counting the day the last job is finished.

Examples

Example 1

Input
jobs = [1, 1], gap = 1
Output
3

The first job goes on day one. The second is the same kind and one whole day has to pass, so day two is out and it goes on day three.

Example 2

Input
jobs = [1, 2, 3], gap = 5
Output
3

All three jobs are of different kinds, so no wait ever bites and they go on three days running.

Example 3

Input
jobs = [4, 4, 4], gap = 2
Output
7

The three jobs share a kind and two whole days must pass between them, so they go on days one, four and seven.

Constraints

  • 1 <= jobs.length <= 10^5
  • 1 <= jobs[i] <= 10^9
  • 1 <= gap <= jobs.length

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def task_scheduler_i_i(jobs: list[int], gap: int) -> int:
Java
public long taskSchedulerII(int[] jobs, int gap)