码迷,mamicode.com
首页 > 其他好文 > 详细

minimize max gas station

时间:2018-02-08 15:29:59      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:bool   guess   station   div   for   body   asd   col   private   

class Solution {
public:
    double minmaxGasDist(vector<int>& stations, int K) {
        double left = 0.0;
        double right = 1e8;
        while (right - left > 1e-6) {
            const auto mid = left + (right - left) / 2.0;
            if (possible(stations, K, mid)) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return left;
    }
    
private:
    bool possible(const vector<int>& stations, int K, double guess) {
        int sum = 0;
        for (int i = 0; i + 1 < stations.size(); ++i) {
            sum += int((stations[i + 1] - stations[i]) / guess);
        }
        return sum <= K;
    }
};

 

minimize max gas station

标签:bool   guess   station   div   for   body   asd   col   private   

原文地址:https://www.cnblogs.com/jxr041100/p/8431392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!