Problem solution · Python

Time to Cross a Bridge

Time to Cross a Bridge: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
48 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Time to Cross a Bridge, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 48 lines of Python from the credited upstream file 2532.py.
  • The implementation visibly relies on sequence storage, work queue.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeTime to Cross a Bridge · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def findCrossingTime(self, n: int, k: int, time: list[list[int]]) -> int:    ans = 0    # (leftToRight + rightToLeft, i)    leftBridgeQueue = [        (-leftToRight - rightToLeft, -i) for i,        (leftToRight, pickOld, rightToLeft, pickNew) in enumerate(time)]    rightBridgeQueue = []    # (time to be idle, i)    leftWorkers = []    rightWorkers = []     heapq.heapify(leftBridgeQueue)     while n > 0 or rightBridgeQueue or rightWorkers:      # Idle left workers get on the left bridge.      while leftWorkers and leftWorkers[0][0] <= ans:        i = heapq.heappop(leftWorkers)[1]        leftWorkers.pop()        heapq.heappush(leftBridgeQueue, (-time[i][0] - time[i][2], -i))      # Idle right workers get on the right bridge.      while rightWorkers and rightWorkers[0][0] <= ans:        i = heapq.heappop(rightWorkers)[1]        heapq.heappush(rightBridgeQueue, (-time[i][0] - time[i][2], -i))      if rightBridgeQueue:        # If the bridge is free, the worker waiting on the right side of the        # bridge gets to cross the bridge. If more than one worker is waiting        # on the right side, the one with the lowest efficiency crosses first.        i = -heapq.heappop(rightBridgeQueue)[1]        ans += time[i][2]        heapq.heappush(leftWorkers, (ans + time[i][3], i))      elif leftBridgeQueue and n > 0:        # If the bridge is free and no worker is waiting on the right side, and       # at least one box remains at the old warehouse, the worker on the left       # side of the river gets to cross the bridge. If more than one worker       # is waiting on the left side, the one with the lowest efficiency       # crosses first.        i = -heapq.heappop(leftBridgeQueue)[1]        ans += time[i][0]        heapq.heappush(rightWorkers, (ans + time[i][1], i))        n -= 1      else:        # Advance the time of the last crossing worker.        ans = min(leftWorkers[0][0] if leftWorkers and n > 0 else math.inf,                  rightWorkers[0][0] if rightWorkers else math.inf)     return ans 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗