Binary search on time. The feasibility check is a single line: sum up how many trips each bus can complete in time t.
You are given an array time where time[i] is the time it takes the i-th bus to complete one trip. Each bus can make multiple trips. Return the minimum time required for all buses together to complete at least totalTrips trips.
At time t, bus i completes floor(t / time[i]) trips. Sum all buses — if total ≥ totalTrips, time t is feasible. The function is monotonically increasing in t, so we find the first feasible t with binary search.
Upper bound: Use min(time) * totalTrips — this is the time if only the fastest bus ran all trips. The answer is always ≤ this.
class Solution { public long minimumTime(int[] time, int totalTrips) { long left = 1; long right = (long) totalTrips * time[0]; // safe upper bound for (int t : time) right = Math.min(right, (long) t * totalTrips); long boundary = right; while (left <= right) { long mid = left + (right - left) / 2; if (tripsIn(time, mid) >= totalTrips) { boundary = mid; right = mid - 1; } else { left = mid + 1; } } return boundary; } private long tripsIn(int[] time, long t) { long trips = 0; for (int bus : time) trips += t / bus; return trips; } }
class Solution: def minimumTime(self, time: list[int], totalTrips: int) -> int: left = 1 right = min(time) * totalTrips # safe upper bound boundary = right while left <= right: mid = left + (right - left) // 2 if sum(mid // t for t in time) >= totalTrips: boundary = mid; right = mid - 1 else: left = mid + 1 return boundary