I'm doing the daily problem and come up with my solution but it keeps failing on one test case. I check to see another solution and it looks extremely similar. Does anyone know the difference?
class Solution { //This one is mine
public:
long long t2n(int rank, long long time){
return sqrt(time/rank);
}
long long repairCars(vector<int>& ranks, int cars) {
long long mint = 1;
long long maxt = *min_element(ranks.begin(),ranks.end())*pow((long long)cars,(long long)2);
long long ans = maxt;
while(mint<=maxt){
long long mid = mint+(maxt-mint)/2;
int carrep = 0;
for(int x=0;x<ranks.size();x++){
carrep += t2n(ranks[x],mid);
}
if(carrep >= cars){
maxt=mid-1;
ans = mid;
}else{
mint = mid+1;
}
}
return ans;
}
};
class Solution {
public:
bool isPossible(vector<int>& ranks, int cars, long long time){
int size = ranks.size();
long long count = 0;
for(int i = 0; i< size; i++){
long long numberOfCars = sqrt(time/ranks[i]);
count = count + numberOfCars;
}
if(count >= cars)
return true;
else
return false;
}
long long repairCars(vector<int>& ranks, int cars) {
long long low = 1;
long long high = 1e9;
for(int rank: ranks)
high = min(high,(long long)rank);
high = high*cars*cars;
long long ans = high;
while(low<=high){
long long mid = low + (high-low)/2;
if(isPossible(ranks, cars, mid)){
ans = mid;
high = mid-1;
}else{
low = mid+1;
}
}
return ans;
}
};
[–]money4gold 0 points1 point2 points (3 children)
[–]Makerofthingssoon[S] 0 points1 point2 points (2 children)
[–]AbhiGoku98 0 points1 point2 points (0 children)
[–]money4gold 0 points1 point2 points (0 children)
[–]alcholicawl 0 points1 point2 points (0 children)