Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Time to Cross a Bridge

XPChallenge Points: 30
levelLevel: Hard

There are k workers responsible for transferring n boxes from a warehouse on the right side to a warehouse on the left side. You are given integers n and k, along with a 2D array time of size k x 4. For each worker i, the array entry:

time[i] = [toRight, pickBox, toLeft, dropBox]

means:

  • The worker takes toRight minutes to cross from left to right.

  • Takes pickBox minutes to take one box on the right side.

  • Takes toLeft minutes to cross back to the left.

  • Takes dropBox minutes to place the box inside the left warehouse.


Workers have efficiency ranking:
A worker i is considered less efficient than worker j if:

  • toRight + toLeft is greater for worker i, or

  • They are equal, but i has a bigger index than j.


Bridge Rules

  • Only one worker can cross at any time.

  • When the bridge is free:

    • Give priority to the least efficient worker currently on the right side (returning with a box).

    • If no one is waiting on the right, then send the least efficient worker from the left side.

  • Do not send new workers to the right if enough workers are already on the right side to finish the remaining boxes.

Return the time when the final box arrives on the left (not when it is dropped/placed).

Example 1:

Input: n = 2 k = 2 time = [[2,3,2,1],[3,1,4,2]]

Output: 12

Explanation:

The slower workers get prioritized when crossing. After coordinating movements, the second box reaches the left at minute 12.

Example 2:

Input: n = 4 k = 3 time = [[1,2,3,1],[2,3,2,2],[3,1,4,1]]

Output: 19

Explanation:

Movement follows the rule: returning workers get priority, and less efficient workers cross earlier. The fourth box reaches left at minute 19.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result