Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2532: Time to Cross a Bridge

In this guide, we solve Leetcode #2532 Time to Cross a Bridge in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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] = [righti, picki, lefti, puti].

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Array, Simulation, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

Example

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.

Python Solution

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))

Complexity

The time complexity is O(n×log⁡k)O(n \times \log k)O(n×logk), and the space complexity is O(k)O(k)O(k). The space complexity is O(k)O(k)O(k).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy