On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., stations[N-1], where N = stations.length.

Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized.

Return the smallest possible value of D.

Example:

Input: stations = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 9
Output: 0.500000

Note:

  1. stations.length will be an integer in range [10, 2000].
  2. stations[i] will be an integer in range [0, 10^8].
  3. K will be an integer in range [1, 10^6].
  4. Answers within 10^-6 of the true value will be accepted as correct.

也是用了二分查找,找的是中位数。

Runtime:28ms  Beats: 81.77%

class Solution {
public:
double minmaxGasDist(vector<int>& stations, int K) {
double left = 0.0, right = 1000000.0, mid = 0.0;
while (left + 0.0000001 < right) {
mid = left + (right - left) / 2.0;
int cnt = ;
for (int i = ; i < stations.size() - ; i++) {
cnt += (stations[i + ] - stations[i]) / mid;
}
if (cnt <= K) right = mid;
else left = mid;
}
return left;
}
};

LC 774. Minimize Max Distance to Gas Station 【lock,hard】的更多相关文章

  1. [LeetCode] 774. Minimize Max Distance to Gas Station 最小化加油站间的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  2. LeetCode - 774. Minimize Max Distance to Gas Station

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

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

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  4. LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

  5. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  7. LC 683. K Empty Slots 【lock,hard】

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  8. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  9. LC 727. Minimum Window Subsequence 【lock,hard】

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

随机推荐

  1. vue 入门1 组件管理

    全局 组件.局部组件 // Vue.component('todo-list',{ // template:'<li >item</li>' // }); //全局 // va ...

  2. Linux用户组管理及用户权限4

    权限管理:    ls -l        rwxrwxrwx:            左三位:定义user(owner)的权限            中三位:定义group的权限           ...

  3. Task扩展方法取消操作

    /// <summary> /// 任务扩展,传入取消操作 /// </summary> public static class TaskExtensionDemo { //因 ...

  4. java-接口—策略模式

    策略模式,就是不同类继承相同的接口,实现不同的策略.

  5. lodash 中 remove

    var obj = { "objectiveDetailId": 285, "objectiveId": 29, "number": 1, ...

  6. ABC007D Small Multiple[最短路]

    题意:求$K$的倍数中数位和的最小值. 一开始有一种思路:由于产生答案的数字可能非常大,不便枚举,考虑转化为构造一个数字可以有$x\mod k=0$.然后二分答案数位和,数位DP检验是否存在,但是由于 ...

  7. vue项目 WebViewJavascriptBridge 适配android和ios

    前言 最近在app 原生页面 嵌套 做Vue 的H5,混合开发,当然原生和Vue 交互方面当然用到 WebViewJavascriptBridge 这个东西啦, 当然在用到的时候也有问题,可以参考大佬 ...

  8. python脚本攻略之log日志

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

  9. JDK8日期处理API(转)

    转载地址:http://www.cnblogs.com/comeboo/p/5378922.html 转载地址:http://blog.csdn.net/hspingcc/article/detail ...

  10. 计算机HDMI端口与电视机相连

     造冰箱的大熊猫@cnblogs 2019/2/27 打算通过HDMI接口将计算机桌面投影到电视机上,结果遇到问题,折腾了好一阵才搞定.现将这些问题记录下来 1.设备环境 计算机:使用Ubuntu 1 ...