Minimum Time to Complete Trips - Problem

Imagine you're managing a bus terminal where multiple buses operate on different schedules. Each bus has its own trip duration, and they all work independently to serve passengers.

You are given an array time where time[i] represents the time it takes for the i-th bus to complete one round trip. Each bus can immediately start its next trip after finishing the current one, operating continuously without any downtime.

Your goal is to find the minimum time required for all buses combined to complete at least totalTrips trips. Think of it as finding the optimal scheduling point where your bus fleet achieves the required service level.

Example: If you have buses with trip times [1, 2, 3] and need 5 total trips, you need to find when the combined trips from all buses first reaches or exceeds 5.

Input & Output

example_1.py โ€” Basic Case
$ Input: time = [1, 2, 3], totalTrips = 5
โ€บ Output: 3
๐Ÿ’ก Note: At time 3: Bus 1 completes 3 trips, Bus 2 completes 1 trip, Bus 3 completes 1 trip. Total = 5 trips.
example_2.py โ€” Single Bus
$ Input: time = [2], totalTrips = 1
โ€บ Output: 2
๐Ÿ’ก Note: Only one bus that takes 2 time units per trip. To complete 1 trip, we need exactly 2 time units.
example_3.py โ€” Multiple Identical Buses
$ Input: time = [5, 5, 5], totalTrips = 9
โ€บ Output: 15
๐Ÿ’ก Note: At time 15: each bus completes 3 trips (15/5 = 3), so total is 3 ร— 3 = 9 trips.

Visualization

Tap to expand
๐ŸšŒ Bus Terminal OptimizationBinary Search: Finding Optimal ScheduleSearch Space: [1, 15] โ†’ Testing mid-point strategiesBus A1 hour/tripFastBus B2 hours/tripMedBus C3 hours/tripSlowTarget: 5 Total TripsTime 3: Bus A (3) + Bus B (1) + Bus C (1) = 5 trips โœ“Minimum time found: 3 hoursBinary Search: O(log n) efficiency vs O(n) brute force
Understanding the Visualization
1
Initialize Fleet
Set up buses with their respective trip times
2
Binary Search Setup
Establish search bounds for possible completion times
3
Test Time Points
For each candidate time, calculate total possible trips
4
Narrow Search
Adjust bounds based on whether target is achievable
5
Find Minimum
Converge to the earliest time meeting requirements
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic property (if time T works, then T+1 also works) enables binary search, reducing time complexity from O(T*n) to O(n*log(T)) where T is the answer.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(T * n)

T is the minimum time needed (could be very large), n is number of buses

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for calculations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค time.length โ‰ค 105
  • 1 โ‰ค time[i], totalTrips โ‰ค 107
  • All buses operate independently and continuously
  • Answer will fit in a 64-bit signed integer
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
68.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen