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.length
will be an integer in range[10, 2000]
.stations[i]
will be an integer in range[0, 10^8]
.K
will be an integer in range[1, 10^6]
.- Answers within
10^-6
of 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 ...
随机推荐
- 阿里最新热修复Sophix与QQ超级补丁和Tinker的实现与总结
2015年以来,Android开发领域里对热修复技术的讨论和分享越来越多,同时也出现了一些不同的解决方案,如QQ空间补丁方案.阿里AndFix以及微信Tinker(Bugly sdk也集成Tikner ...
- SpringMVC拦截器详解
拦截器是每个Web框架必备的功能,也是个老生常谈的主题了. 本文将分析SpringMVC的拦截器功能是如何设计的,让读者了解该功能设计的原理. 重要接口及类介绍 1. HandlerExecution ...
- .net源码调试 http://referencesource.microsoft.com/
其实关于.net源码调试 网上的资料已经很多了,我以前转载的文章有 VS2010下如何调试Framework源代码(即FCL) 和 如何使你的应用程序调试进.NET Framework 4.5源代码内 ...
- alter日志报WARNING: too many parse errors
数据库版本:12.2.0 操作系统版本:RHEL7.2 最近观察到一个数据库alert日志老是报硬解析太多错误,且对应的sql语句都是查看数据字典表: 2017-06-16T08:46:46.4174 ...
- struts2:多业务方法的处理(动态调用,DMI)
struts2支持调用指定Action类中某一个业务方法.如果没有指定,则调用execute方法. 1. 第一种实现方式,通过URL叹号参数 1.1 创建Action类,带多个方法 package c ...
- Atitit mybatis快速开发 的sql api接口
Atitit mybatis快速开发 的sql api接口 1.1. sql模式 开发速度大大快与 映射模式1 1.2. MyBatis Mapper1 1.2.1. 代码2 1.2.2. 原理2 1 ...
- 设置全局git忽略文件 gitconfig
cat ~/.gitconfig [user] email = yuanhuikai@liquidnetwork.com name = yuanhuikai[core] excludesfile = ...
- 安装oracle遇到的故障
安装oracle遇到的故障 安装oracle遇到的故障总结 os:centos4.7(64位)db版本:oracle10.0.2.1(64位) 这次安装oracle又遇到点小问题,每次都是遇到点小问题 ...
- 【emWin】例程二十二:窗口对象——Framewin
简介: 框架窗口为您的应用提供一个PC 应用程序的窗口外观.这些窗口由周围框架.标题栏和用户区组成. 触摸校准(上电可选择是否进入校准界面) 截图 实验指导书及代码包下载: 链接:http://pan ...
- sql server 获取动态sql输出结果
不带输出结果 我们一般会这样写 例子:一个输出6位递增号码结果 ALTER proc GetCode ), ) as declare @sqlstring nvarchar(max) set @sql ...