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