All problems
1011MediumArrayBinary Search

How Long the Bay Needs to Clear the Queue

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2594Minimum Time to Repair Cars

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 repair bay has mechanics whose ranks read ranks. A mechanic of rank r needs r minutes to finish one machine, four times r minutes to finish two, nine times r to finish three, and in general r times the square of however many machines that mechanic takes on.

The mechanics all work at the same time and none of them helps another. There are machines machines waiting, all alike, and every one has to be finished.

Return the fewest minutes after which the queue can be cleared.

Examples

Example 1

Input
ranks = [3, 7], machines = 5
Output
28

In twenty-eight minutes the rank-three mechanic gets through three machines, needing twenty-seven of them, and the rank-seven mechanic gets through two, needing all twenty-eight. A minute less and the slower mechanic drops to a single machine, leaving the queue one short.

Example 2

Input
ranks = [2, 2], machines = 4
Output
8

Two mechanics of the same rank split the four machines evenly, and two machines apiece takes eight minutes.

Example 3

Input
ranks = [1], machines = 1
Output
1

One machine for the fastest rank there is takes a single minute.

Constraints

  • 1 <= ranks.length <= 10^5
  • 1 <= ranks[i] <= 100
  • 1 <= machines <= 10^6

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 repair_cars(ranks: list[int], machines: int) -> int:
Java
public long repairCars(int[] ranks, int machines)
September 7
Apply