Minimum Time to Complete Trips
Medium
Topics
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
Input: nums = [1,2,3], target = 5 Output: 3
Example 2
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
- Search
tover[1, min(nums) * target]. - For the midpoint, sum
t // xacross all buses. - If the total reaches
target, the time is sufficient, so movehidown; otherwise moveloup. - 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 3Complexity
- 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) * targetreaches10⁷ × 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) * targetas the bound. Still correct, just a larger search space for nothing — and it makes the complexity you quote wrong. - Returning the midpoint rather than
loafter the loop. With thewhile lo < hiandhi = midform, 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 loimport java.util.*;
class Solution {
public int minimumTime(int[] nums, int target) {
int mn = nums[0];
for (int x : nums) mn = Math.min(mn, x);
long lo = 1, hi = (long) mn * target;
while (lo < hi) {
long mid = (lo + hi) / 2;
if (trips(nums, mid) >= target) hi = mid;
else lo = mid + 1;
}
return (int) lo;
}
long trips(int[] nums, long t) {
long total = 0;
for (int x : nums) total += t / x;
return total;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minimumTime(vector<int>& nums, int target) {
long long mn = nums[0];
for (int x : nums) mn = min(mn, (long long) x);
long long lo = 1, hi = mn * (long long) target;
auto trips = [&](long long t) {
long long total = 0;
for (int x : nums) total += t / x;
return total;
};
while (lo < hi) {
long long mid = (lo + hi) / 2;
if (trips(mid) >= target) hi = mid;
else lo = mid + 1;
}
return (int) lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var minimumTime = function(nums, target) {
let mn = nums[0];
for (const x of nums) mn = Math.min(mn, x);
let lo = 1, hi = mn * target;
const trips = (t) => {
let total = 0;
for (const x of nums) total += Math.floor(t / x);
return total;
};
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (trips(mid) >= target) hi = mid;
else lo = mid + 1;
}
return lo;
};