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 leasttotalTripstrips.
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:
1
2
3
4
5
6
7
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.
We want to find the minimum time t such that the total number of trips completed by all buses in t time is at least totalTrips. For a given t, the number of trips a bus with time[i] can make is t // time[i]. We can use binary search to find the smallest t that works.
classSolution {
public:longlong minimumTime(vector<int>& time, int totalTrips) {
longlong l =1, r =1LL**max_element(time.begin(), time.end()) * totalTrips, ans = r;
while (l <= r) {
longlong m = l + (r - l) /2, trips =0;
for (int t : time) trips += m / t;
if (trips >= totalTrips) ans = m, r = m -1;
else l = m +1;
}
return ans;
}
};
classSolution {
publiclongminimumTime(int[] time, int totalTrips) {
long l = 1, r = (long) Arrays.stream(time).max().getAsInt() * totalTrips, ans = r;
while (l <= r) {
long m = l + (r - l) / 2, trips = 0;
for (int t : time) trips += m / t;
if (trips >= totalTrips) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funminimumTime(time: IntArray, totalTrips: Int): Long {
var l = 1Lvar r = time.maxOrNull()!!.toLong() * totalTrips
var ans = r
while (l <= r) {
val m = l + (r - l) / 2var trips = 0Lfor (t in time) trips += m / t
if (trips >= totalTrips) {
ans = m
r = m - 1 } else {
l = m + 1 }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defminimumTime(self, time: list[int], totalTrips: int) -> int:
l, r =1, max(time) * totalTrips
ans = r
while l <= r:
m = (l + r) //2 trips = sum(m // t for t in time)
if trips >= totalTrips:
ans = m
r = m -1else:
l = m +1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnminimum_time(time: Vec<i32>, total_trips: i32) -> i64 {
let (mut l, mut r) = (1i64, *time.iter().max().unwrap() asi64* total_trips asi64);
letmut ans = r;
while l <= r {
let m = l + (r - l) /2;
let trips = time.iter().map(|&t| m / t asi64).sum::<i64>();
if trips >= total_trips asi64 {
ans = m;
r = m -1;
} else {
l = m +1;
}
}
ans
}
}