Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: time = [1,2,3], totalTrips = 5 Output: 3 Explanation: - At time t = 1, the number of trips completed by each bus are [1,0,0]. The total number of trips completed is 1 + 0 + 0 = 1. - At time t = 2, the number of trips completed by each bus are [2,1,0]. The total number of trips completed is 2 + 1 + 0 = 3. - At time t = 3, the number of trips completed by each bus are [3,1,1]. The total number of trips completed is 3 + 1 + 1 = 5. So the minimum time needed for all buses to complete at least 5 trips is 3.
Example 2:
Input: time = [2], totalTrips = 1 Output: 2 Explanation: There is only one bus, and it will complete its first trip at t = 2. So the minimum time needed to complete 1 trip is 2.
Constraints:
1 <= time.length <= 1051 <= time[i], totalTrips <= 107Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[1,2,3] 5
[2] 1
maximum-candies-allocated-to-k-children)minimum-speed-to-arrive-on-time)minimized-maximum-of-products-distributed-to-any-store)maximum-running-time-of-n-computers)maximum-number-of-robots-within-budget)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2187: Minimum Time to Complete Trips
class Solution {
public long minimumTime(int[] time, int totalTrips) {
int mi = time[0];
for (int v : time) {
mi = Math.min(mi, v);
}
long left = 1, right = (long) mi * totalTrips;
while (left < right) {
long cnt = 0;
long mid = (left + right) >> 1;
for (int v : time) {
cnt += mid / v;
}
if (cnt >= totalTrips) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
// Accepted solution for LeetCode #2187: Minimum Time to Complete Trips
func minimumTime(time []int, totalTrips int) int64 {
mx := slices.Min(time) * totalTrips
return int64(sort.Search(mx, func(x int) bool {
cnt := 0
for _, v := range time {
cnt += x / v
}
return cnt >= totalTrips
}))
}
# Accepted solution for LeetCode #2187: Minimum Time to Complete Trips
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
mx = min(time) * totalTrips
return bisect_left(
range(mx), totalTrips, key=lambda x: sum(x // v for v in time)
)
// Accepted solution for LeetCode #2187: Minimum Time to Complete Trips
/**
* [2187] Minimum Time to Complete Trips
*
* You are given an array time where time[i] denotes the time taken by the i^th 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.
*
* Example 1:
*
* Input: time = [1,2,3], totalTrips = 5
* Output: 3
* Explanation:
* - At time t = 1, the number of trips completed by each bus are [1,0,0].
* The total number of trips completed is 1 + 0 + 0 = 1.
* - At time t = 2, the number of trips completed by each bus are [2,1,0].
* The total number of trips completed is 2 + 1 + 0 = 3.
* - At time t = 3, the number of trips completed by each bus are [3,1,1].
* The total number of trips completed is 3 + 1 + 1 = 5.
* So the minimum time needed for all buses to complete at least 5 trips is 3.
*
* Example 2:
*
* Input: time = [2], totalTrips = 1
* Output: 2
* Explanation:
* There is only one bus, and it will complete its first trip at t = 2.
* So the minimum time needed to complete 1 trip is 2.
*
*
* Constraints:
*
* 1 <= time.length <= 10^5
* 1 <= time[i], totalTrips <= 10^7
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-time-to-complete-trips/
// discuss: https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimum_time(time: Vec<i32>, total_trips: i32) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2187_example_1() {
let time = vec![1, 2, 3];
let total_trips = 5;
let result = 3;
assert_eq!(Solution::minimum_time(time, total_trips), result);
}
#[test]
#[ignore]
fn test_2187_example_2() {
let time = vec![2];
let total_trips = 1;
let result = 2;
assert_eq!(Solution::minimum_time(time, total_trips), result);
}
}
// Accepted solution for LeetCode #2187: Minimum Time to Complete Trips
function minimumTime(time: number[], totalTrips: number): number {
let left = 1n;
let right = BigInt(Math.min(...time)) * BigInt(totalTrips);
while (left < right) {
const mid = (left + right) >> 1n;
const cnt = time.reduce((acc, v) => acc + mid / BigInt(v), 0n);
if (cnt >= BigInt(totalTrips)) {
right = mid;
} else {
left = mid + 1n;
}
}
return Number(left);
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.