Find minimum time to cross bridge with flashlight
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving and optimization skills, including modeling constrained state transitions, reasoning about trade-offs under a single shared resource, and minimizing aggregate cost.
Constraints
- 0 <= n <= 200000
- 1 <= t[i] <= 10^9
- The answer fits in a 64-bit signed integer
Examples
Input: ([],)
Expected Output: 0
Explanation: No one needs to cross, so the total time is 0.
Input: ([3],)
Expected Output: 3
Explanation: Only one person crosses once.
Input: ([4, 7],)
Expected Output: 7
Explanation: Both cross together, taking the slower person's time: 7.
Input: ([1, 2, 5, 10],)
Expected Output: 17
Explanation: Optimal sequence: 1 and 2 cross (2), 1 returns (1), 5 and 10 cross (10), 2 returns (2), 1 and 2 cross (2). Total = 17.
Input: ([2, 2, 2, 2],)
Expected Output: 10
Explanation: Every crossing or return takes 2. Minimum total is 2 + 2 + 2 + 2 + 2 = 10.
Input: ([8, 1, 2, 6, 12],)
Expected Output: 26
Explanation: After sorting to [1, 2, 6, 8, 12], optimally move 8 and 12 first using the two fastest as helpers, then finish the remaining three. Total = 26.
Hints
- Sort the crossing times first. Once sorted, the important decision is how to move the two slowest remaining people.
- For the two slowest people, compare these two strategies: send the two fastest as helpers, or send only the fastest back and forth.