Ctrl K

Busiest Workers in a Ring

Hard
Workers numbered 0 to workers - 1 stand in a ring. Each of them handles at most one task at a time. Tasks come one by one: task i (0-indexed) appears at time arrivals[i] and takes durations[i] units of time. The array arrivals is strictly increasing. When task i arrives, it is assigned like this: - if every worker is busy, the task is lost; - otherwise worker i % workers takes it if they are free; - if that worker is busy, the next ones around the ring are tried: i % workers + 1, then the one after, and so on, wrapping from workers - 1 back to 0. The first free worker gets the task. A worker who takes a task at time t with duration d is free again from time t + d on (and can take a task arriving exactly then). Return the numbers of all workers who completed the most tasks, in any order.
Example 1
workers=2, arrivals=[1, 2, 3, 4], durations=[4, 1, 1, 1] → [1]

Task 0 goes to worker 0 until time 5. Task 1 goes to worker 1 (until 3). Task 2 prefers worker 0, who is busy, so it moves around the ring to worker 1, free again exactly at time 3. Task 3 prefers worker 1 and gets them at time 4. Worker 1 finished three tasks, worker 0 one.

Example 2
workers=3, arrivals=[1, 2, 3, 4], durations=[10, 10, 10, 10] → [0, 1, 2]

The first three tasks take every worker; the fourth arrives while all are busy and is lost. Each worker did one task, so all three are the busiest.

Example 3
workers=4, arrivals=[1, 3, 5, 7, 9], durations=[1, 1, 1, 1, 1] → [0]

Everyone is free, so every task lands on its preferred worker i % 4: tasks 0 and 4 both go to worker 0, who ends up with two.

Constraints
  • 1 ≤ workers ≤ 10⁵
  • 1 ≤ arrivals.length = durations.length ≤ 10⁵
  • 1 ≤ arrivals[i], durations[i] ≤ 10⁹
  • arrivals is strictly increasing
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHalve or Double HardCity Block Outline HardGrowing Capital Project by Project HardMost Orders by Their Deadlines HardNarrowest Range Touching Every List Hard
loading Python…
Input
workers=2, arrivals=[1, 2, 3, 4], durations=[4, 1, 1, 1]
Expected
[1]

Run checks the first 3 tests in your browser. Submit checks all 173 tests on the server, hidden ones included.