LeetCode - 774. Minimize Max Distance to Gas Station
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:
stations.lengthwill be an integer in range[10, 2000].stations[i]will be an integer in range[0, 10^8].Kwill be an integer in range[1, 10^6].- Answers within 
10^-6of the true value will be accepted as correct. 
贪心肯定不对,参考discuss,假设一个结果,然后对结果进行二分逼近。
public double minmaxGasDist(int[] stations, int K) {
        int LEN = stations.length;
        double left = 0, right = stations[LEN - 1] - stations[0], mid = 0;
        while (right >= left + 0.000001) {
            mid = right - (right - left) / 2;
            int cnt = 0;
            for (int i = 0; i < LEN - 1; i++) {
                cnt += Math.ceil((stations[i + 1] - stations[i]) / mid) - 1; //重点理解代码,d_i / (cnt_i + 1) <= mid
            }
            if (cnt > K) {
                left = mid;
            } else {
                right = mid;
            }
        }
        return mid;
    }
LeetCode - 774. Minimize Max Distance to Gas Station的更多相关文章
- [LeetCode] 774. Minimize Max Distance to Gas Station 最小化加油站间的最大距离
		
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
 - LC 774. Minimize Max Distance to Gas Station 【lock,hard】
		
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
 - [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
		
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
 - leetcode 刷题之路 68 Gas Station
		
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
 - 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
		
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
 - [LeetCode] Gas Station
		
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
 - leetcode@ [134] Gas station (Dynamic Programming)
		
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
 - [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。
		
LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...
 - [LeetCode] Gas Station 加油站问题
		
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
 
随机推荐
- JavaScript(ES5)使用保留字作函数名
			
ES5同意直接使用保留字作为属性名.但却不同意直接使用保留字作为函数名 设现有类NSMap,若要给NSMap的原型加delete方法,如 function NSMap(){ } NSMap.proto ...
 - android手势识别ViewFlipper触摸动画
			
使用ViewFlipper来将您要来回拖动的View装在一起,然 后与GestureDetector手势识别类来联动,确定要显示哪个View,加上一点点动画效果即可.比如当手指向左快速滑动时跳转到上一 ...
 - VMWare虚拟机中CPU过高的问题
			
在VMWare中按默认方式创建的虚拟机,安装的Windows Server 2016 x64操作系统.可打开一个稍微大一点的程序CPU就飙到90%以上,自然整个系统操作起来很卡. 在VMWare中看到 ...
 - npm install node-sass失败
			
Cannot download "https://github.com/sass/node-sass/releases/download/v3.8.0/win32-x64-46_bindin ...
 - Spark机器学习(12):神经网络算法
			
1. 神经网络基础知识 1.1 神经元 神经网络(Neural Net)是由大量的处理单元相互连接形成的网络.神经元是神经网络的最小单元,神经网络由若干个神经元组成.一个神经元的结构如下: 上面的神经 ...
 - ftrace 示例
			
假设debugfs已经挂载到了/sys/kernel/debug目录下,下面的小脚本用来抓取unlink系统调用的耗时: cd /sys/kernel/debug/tracing echo funct ...
 - JAVA Spring boot相关技巧
			
1. 注册多实例.@Scope("prototype") 2. 手工方式获取注册的实例. @Autowired private ServletContext servletCont ...
 - 在Android上启用Kiosk模式
			
我们的云帆机器人(上面运行的安卓程序)有一个线下场景是商场,由于商场人多,总会遇到一些用户在我们的app里乱点,然后会跳出程序进入到系统设置的一些界面,这样很不友好. 比如程序中有一些需要输入文字的地 ...
 - SED单行脚本快速参考(Unix 流编辑器)(转)
			
sed.sourceforge.net被封杀,特在此处贴上官方的sed 使用说明文档 SED单行脚本快速参考(Unix 流编辑器) 2005年12月29日 英文标题:USEFUL ONE-LINE S ...
 - SNF快速开发平台MVC-富文本控件集成了百度开源项目editor
			
一.效果如下: 二.在框架当中调用代码如下: 1.在js里配置如下: <script type="text/javascript"> var viewModel =fu ...