[LeetCode] 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 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
long long minimumTime(vector<int>& time, int totalTrips) {
sort(time.begin(), time.end());
long long l = 1, r = LLONG_MAX;
long long res = LLONG_MAX;
while(l <= r) {
long long sum = 0;
long long m = l + (r- l) / 2; //target time;
for(auto t : time) {
sum += m / t;
if(sum >= totalTrips) break;
}
if(sum >= totalTrips) {
res = min(res, m);
r = m - 1;
} else {
l = m + 1;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/27/PS/LeetCode/minimum-time-to-complete-trips/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.