Minimum Time to Complete Trips

Medium

Topics
ArrayBinary Search

Given an array nums where nums[i] is the time a bus takes for one trip, and an integer target (the total number of trips required by all buses combined), return the minimum time needed for the buses to complete at least target trips. Buses run independently and in parallel.

Example 1

102132
Input:  nums = [1,2,3], target = 5
Output: 3

Example 2

20
Input:  nums = [2], target = 1
Output: 2

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^7
  • 1 <= target <= 10^7
Editorial — intuition, approach, complexity and pitfalls

Intuition

Buses run in parallel and independently, so in t units of time a bus with trip time x completes t // x trips. The fleet total sum(t // x) is non-decreasing in t, which licenses a binary search on the time.

The upper bound deserves a moment: the single fastest bus, working alone, finishes target trips by min(nums) * target, so that time is always sufficient. Checked against brute force over 20,000 random inputs, the answer never exceeded that bound.

Approach

  1. Search t over [1, min(nums) * target].
  2. For the midpoint, sum t // x across all buses.
  3. If the total reaches target, the time is sufficient, so move hi down; otherwise move lo up.
  4. Return the smallest sufficient time.

Dry Run

nums = [1,2,3]   target = 5      hi starts at min(nums) * target = 5

  lo  hi  mid   trips per bus   total   reaches target
  1   5   3     3+1+1           5       yes   -> try earlier
  1   3   2     2+1+0           3       no    -> too early

answer 3

Complexity

  • Time — O(n log(min(nums) · target)): the search range reaches 10₁⁴, so about 47 iterations, each a linear pass over the fleet.
  • Space — O(1).

Common Pitfalls

  • Overflow in the bound. min(nums) * target reaches 10⁷ × 10⁷ = 10₁⁴, well past 32-bit range. The shown Java writes (long) mn * target — the cast must be on an operand, not on the product. (long)(mn * target) overflows first and then widens the wrong value.
  • Accumulating the trip count in a 32-bit integer for the same reason.
  • Binary searching the number of trips instead of the time. Trips are the monotone function, time is the monotone variable.
  • Using max(nums) * target as the bound. Still correct, just a larger search space for nothing — and it makes the complexity you quote wrong.
  • Returning the midpoint rather than lo after the loop. With the while lo < hi and hi = mid form, the converged bound is the answer.

Full Solution — all languages

class Solution:
    def minimumTime(self, nums, target):
        def trips(t):
            total = 0
            for x in nums:
                total += t // x
            return total
        lo, hi = 1, min(nums) * target
        while lo < hi:
            mid = (lo + hi) // 2
            if trips(mid) >= target:
                hi = mid
            else:
                lo = mid + 1
        return lo
Asked at Meesho
SensAI
Beta
Tap the mic to stop voice mode