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], ..., 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.
也是用了二分查找,找的是中位数。
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】的更多相关文章
- [LeetCode] 774. Minimize Max Distance to Gas Station 最小化加油站间的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- LeetCode - 774. Minimize Max Distance to Gas Station
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 ...
- 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 ...
- 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 ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Java学习笔记【十二、网络编程】
原计划的学习结束时间是3月4日,目前看来已经延迟了,距离低标还差一些,多方面原因,也不找借口,利用周末赶赶进度,争取本周末把低标完成吧! 参考: http://www.runoob.com/java/ ...
- windows 下Nginx 入门
验证配置是否正确: nginx -t 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx: ...
- 分布式之协调服务Zookeeper专题第一讲
写在前面: 再谈架构演进: 1.单体架构->tomcat(war) 演进:水平拆分(服务拆分,(用户服务,订单服务,商品服务)) 涉及问题:1).服务通信(webservice) 2).三态问题 ...
- MacBook Pro实现共享屏幕(多台mac之间)
1 开启允许所有用户控制屏幕 设置— 共享 2 在另一台MacBook Pro里打开finder窗口,并设置好network显示边栏 3 另一台机器发送请求控制 找 ...
- 关于一个指针的题目解析(a,&a,(int*)&a,(int*)((char*)&a + 4))
#include <stdio.h> void main() { ] = {}; printf(]); printf("\n"); printf("a[1] ...
- 使用cJSON解析JSON
cJSON获取数组元素的每个值 { "operType": 0x5, "field": ["time","matchRule&qu ...
- 查看电脑物理地址(MAC)方法
首先打开电脑,按ctrl+R键,将会出现以下界面 然后直接点击确认即可,会出现管理员界面,如下 我们现在有两种查看MAC地址的方法: 方法一:.直接输入ipconfig/all(或者输入ipconfi ...
- BZOJ 1003 最短路dp
物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪.由于各种因 ...
- Cobbler——无人值守安装多种版本多种配置操作系统
Cobbler Web 图形界面也能实现类似的导入功能,可以参见下一节举了一个Cobbler Web的用法: cobbler 添加网络同步仓库(Reposync用法)
- linux 下 svn 重新定位 url
linux下重新定位SVN URL方法: 如果更换了SVN服务器,就需要重新定位,指向新的SVN URL. 重新定位命令:svn switch --relocate 原svn地址 新svn地址 ...