All problems
1064MediumArrayHash TableSimulation

How Many Days the Run of Jobs Takes

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2365Task Scheduler II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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