2dbi

Bridge crossing riddle

viaLeetCode

Problem Four people must cross a bridge at night with one torch. At most two may cross at a time, a crossing party moves at the slower member's speed, and the torch must accompany every crossing (someone must bring it back). With individual times of 1, 2, 5, and 10 minutes, find the minimum total time for all four to cross.

Input / Output

  • Input: crossing times (classically 1, 2, 5, 10).
  • Output: minimum total time and the crossing schedule.

Example

  • Answer for (1,2,5,10): 17 minutes — 1&2 cross (2), 1 returns (1), 5&10 cross (10), 2 returns (2), 1&2 cross (2).

Expected approach

  • The greedy trap is always escorting with the fastest person (1&10, 1 back, 1&5, 1 back, 1&2 = 19). The insight: send the two slowest together so their times overlap, using the two fastest as ferries. General strategy per slowest pair: compare "fastest escorts each" (t1 + t_slow pairs) vs "two fastest ferry the two slowest" (t2·2 + t1 + t_slow) and take the cheaper. Interviewers want the reasoning and the comparison of strategies, not just 17.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account