Minimum Time to Complete Trips - Problem

You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip.

Each bus can make multiple trips successively; that is, the next trip can start immediately after completing the current trip. Also, each bus operates independently; that is, the trips of one bus do not influence the trips of any other bus.

You are also given an integer totalTrips, which denotes the number of trips all buses should make in total. Return the minimum time required for all buses to complete at least totalTrips trips.

Input & Output

Example 1 — Basic Case
$ Input: time = [1,2,3], totalTrips = 5
Output: 3
💡 Note: At time 3: Bus 0 completes 3/1=3 trips, Bus 1 completes 3/2=1 trip, Bus 2 completes 3/3=1 trip. Total: 3+1+1=5 trips.
Example 2 — Slower Buses
$ 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 — Multiple Fast Buses
$ Input: time = [1,1,1], totalTrips = 10
Output: 4
💡 Note: At time 4: Each of the 3 buses completes 4 trips, totaling 12 trips which exceeds the required 10.

Constraints

  • 1 ≤ time.length ≤ 105
  • 1 ≤ time[i], totalTrips ≤ 107

Visualization

Tap to expand
Minimum Time to Complete TripsBus 1Bus 2Bus 31 time/trip2 time/trip3 time/trip3 trips at t=31 trip at t=31 trip at t=3Need 5 total tripsAt time 3: 3 + 1 + 1 = 5 trips ✓Minimum Time: 3
Understanding the Visualization
1
Input
Buses with times [1,2,3] need 5 total trips
2
Process
Find minimum time where trips ≥ 5
3
Output
At time 3: buses complete 3+1+1=5 trips
Key Takeaway
🎯 Key Insight: Binary search on time to efficiently find the minimum duration needed for all buses to complete the required trips
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
23.4K Views
Medium Frequency
~25 min Avg. Time
892 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