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;
}
};

最新文章

  1. 看看国外的javascript题目,你能全部做对吗?
  2. java中的权限修饰符的理解
  3. centos创建监控宝采集器及添加插件任务
  4. xcrun: error: active developer path (&quot;/Volumes/Xcode/Xcode-beta.app/Contents/Developer&quot;) does not exist, use `xcode-select --swi
  5. linux各种查看端口号
  6. Python数据结构——链表的实现
  7. ios开发之C语言第4天
  8. HTML5实现坦克大战(一)
  9. ICMP(网际控制报文协议)
  10. 文科生细谈学习Linux系统的重要性
  11. ThinkPHP 5 中AJAX跨域请求头设置方法
  12. Linux进程-命令行参数和环境列表
  13. 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao
  14. JavaScript实用的工具/类库
  15. bash array
  16. 20165223 2017-2018-2《Java程序设计》课程总结
  17. 深入springboot原理——动手封装一个starter
  18. 使用.pth文件扩展python环境路径
  19. 1、Docker概述与安装
  20. Sublime Text Ctags 安装、使用、快捷键

热门文章

  1. 第五篇python进阶之深浅拷贝
  2. Delphi GetCommModemStatus函数
  3. docker使用上的错误
  4. linux常用查看命令
  5. Linux ping route nslookup ifconfig arp traceroute
  6. 两步创建vue全局组件
  7. python 示例代码4
  8. 阿里云端安装mysql
  9. linux 计算机概论 Linux介绍
  10. 快速数论变换NTT模板