-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathminTimeToCompleteTrip.cpp
More file actions
48 lines (39 loc) · 1.2 KB
/
minTimeToCompleteTrip.cpp
File metadata and controls
48 lines (39 loc) · 1.2 KB
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
45
46
47
48
#include <iostream>
#include <vector>
// 2187. Minimum Time to Complete Trips
class Solution {
using lng = long long int;
public:
long long minimumTime(std::vector<int>& arr , int totalTrips) {
lng lowestTime = 1;
lng highestTime = 1e14;
while(lowestTime<highestTime)
{
lng mid = lowestTime + (highestTime-lowestTime)/2;
if([&]()
{
// this lambda will count totalTrips for the given time
// a = [1,2,3] , and at time 3 how many trips we can take?
// 3/1 + 3/2 + 3/3 => 3 + 1 + 1 = 5 Trips
lng totalTrips = 0;
for(const auto& x : arr)
{
// convert it to long long int
lng val = x;
totalTrips += (mid / val);
}
return totalTrips;
}() >= totalTrips)
highestTime = mid;
else
lowestTime = mid+1;
}
return lowestTime;
}
};
int main ()
{
std::vector<int> vec {1,2,3};
int totalTrips {5};
std::cout<<Solution{}.minimumTime(vec, totalTrips)<<std::endl;
}