Advertisement
nikunjsoni

774

May 12th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     double minmaxGasDist(vector<int>& stations, int k) {
  4.         double low = 0, high = 1e8, mid;
  5.        
  6.         while(high - low > 1e-6){
  7.             double mid = (low+high)/2.0;
  8.             if(check(mid, stations, k))
  9.                 high = mid;
  10.             else
  11.                 low = mid;
  12.         }
  13.         return low;
  14.     }
  15.    
  16.     bool check(double dist, vector<int> stations, int k){
  17.         int stops = 0;
  18.         for(int i=0; i<stations.size()-1; i++)
  19.             stops += int(double(stations[i+1]-stations[i])/dist);
  20.         return stops <= k;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement