minimize max gas station

Posted jxr041100

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了minimize max gas station相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

[Lintcode]187. Gas Station/[Leetcode]134. Gas Station

134. Gas Station

134. Gas Station

134.Gas Station

134. Gas Station