-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMinimum Time to Complete Trips.cpp
44 lines (32 loc) · 1.42 KB
/
Minimum Time to Complete Trips.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,mmx,sse2,sse3,sse4")
class Solution {
public:
long long minimumTime(vector<int>& time, int totalTrips) {
long long l = time[0],r = 0;
for(auto x: time){
l = min(l,(long long)x);
r = max(r,(long long)x);
}
r*=totalTrips;
auto ct = [&](long long a){ long long c = 0; for(auto x: time){ c+=(a/x); } return c; };
while(l<=r){
long long m = (l+r)/2;
auto z = ct(m);
if(z>=totalTrips) { r = m-1; }
else if(z < totalTrips){ l = m+1; }
}
return l;
}
};