Skip to main content
Back to problems
#2187
Medium Algorithms

Minimum time to complete trips

Array Binary Search
39.6% acceptance
Feb 25, 2026
3078
195
You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip. Return the minimum time required for all buses to complete at least totalTrips trips.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_time(time: Vec<i32>, total_trips: i32) -> i64 {
    let total_trips = total_trips as i64;
    let mut lo = 1i64;
    let mut hi = *time.iter().min().unwrap() as i64 * total_trips;
    while lo < hi {
      let mid = (lo + hi) / 2;
      let trips: i64 = time.iter().map(|&t| mid / t as i64).sum();
      if trips >= total_trips {
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    lo
  }
}