Time to Cross a Bridge Problem
Description
LeetCode Problem 2532.
There are k workers who want to move n boxes from the right (old) warehouse to the left (new) warehouse. You are given the two integers n and k, and a 2D integer array time of size k x 4 where time[i] = [right_i, pick_i, left_i, put_i].
The warehouses are separated by a river and connected by a bridge. Initially, all k workers are waiting on the left side of the bridge. To move the boxes, the i^th worker can do the following:
- Cross the bridge to the right side in
right_iminutes. - Pick a box from the right warehouse in
pick_iminutes. - Cross the bridge to the left side in
left_iminutes. - Put the box into the left warehouse in
put_iminutes.
The i^th worker is less efficient than the j^th worker if either condition is met:
left_i + right_i > left_j + right_jleft_i + right_i == left_j + right_jandi > j
The following rules regulate the movement of the workers through the bridge:
- Only one worker can use the bridge at a time.
- When the bridge is unused prioritize the least efficient worker (who have picked up the box) on the right side to cross. If not, prioritize the least efficient worker on the left side to cross.
- If enough workers have already been dispatched from the left side to pick up all the remaining boxes, no more workers will be sent from the left side.
Return the elapsed minutes at which the last box reaches the left side of the bridge.
Example 1:
Input: n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]
Output: 6
Explanation:
From 0 to 1 minutes: worker 2 crosses the bridge to the right.
From 1 to 2 minutes: worker 2 picks up a box from the right warehouse.
From 2 to 6 minutes: worker 2 crosses the bridge to the left.
From 6 to 7 minutes: worker 2 puts a box at the left warehouse.
The whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left side of the bridge.
Example 2:
Input: n = 3, k = 2, time = [[1,5,1,8],[10,10,10,10]]
Output: 37
Explanation:

The last box reaches the left side at 37 seconds. Notice, how we do not put the last boxes down, as that would take more time, and they are already on the left with the workers.
Constraints:
1 <= n, k <= 10^4time.length == ktime[i].length == 41 <= left_i, pick_i, right_i, put_i <= 1000
Solution
Approach: Four Heaps + Simulation
Sort the workers by efficiency (left + right, ties by index) so that a larger index always means a less efficient worker, and the heap of waiting workers can just compare indices.
Use four heaps:
wait_in_leftandwait_in_right: max-heaps of indices of workers waiting at each bank (the largest index is the least efficient and goes first).work_in_leftandwork_in_right: min-heaps of(finish time, index)for workers who are putting down or picking up a box.
Keep a clock cur. In every round, move workers whose work has finished by cur into the matching waiting heap. If nobody is waiting to cross (on the left, nobody counts once all n boxes have been picked), jump the clock to the next finish time. Otherwise a worker waiting on the right crosses first (adding the right-to-left time, and if that was the last box, cur is the answer); if there is none, a worker from the left crosses, takes a box and starts picking it up.
Complexity: O((n + k) log k) time and O(k) space, where n is the number of boxes and k the number of workers.
from heapq import heappop, heappush
from math import inf
class Solution:
def findCrossingTime(self, n: int, k: int, time: List[List[int]]) -> int:
time.sort(key=lambda x: x[0] + x[2])
cur = 0
wait_in_left, wait_in_right = [], []
work_in_left, work_in_right = [], []
for i in range(k):
heappush(wait_in_left, -i)
while 1:
while work_in_left:
t, i = work_in_left[0]
if t > cur:
break
heappop(work_in_left)
heappush(wait_in_left, -i)
while work_in_right:
t, i = work_in_right[0]
if t > cur:
break
heappop(work_in_right)
heappush(wait_in_right, -i)
left_to_go = n > 0 and wait_in_left
right_to_go = bool(wait_in_right)
if not left_to_go and not right_to_go:
nxt = inf
if work_in_left:
nxt = min(nxt, work_in_left[0][0])
if work_in_right:
nxt = min(nxt, work_in_right[0][0])
cur = nxt
continue
if right_to_go:
i = -heappop(wait_in_right)
cur += time[i][2]
if n == 0 and not wait_in_right and not work_in_right:
return cur
heappush(work_in_left, (cur + time[i][3], i))
else:
i = -heappop(wait_in_left)
cur += time[i][0]
n -= 1
heappush(work_in_right, (cur + time[i][1], i))