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 ...
随机推荐
- Hadoop_09_HDFS 的 NameNode工作机制
理解NameNode的工作机制尤其是元数据管理机制,以增强对HDFS工作原理的理解,及培养hadoop集群运营中“性能调优” “NameNode”故障问题的分析解决能力 1.NameNode职责: H ...
- API开发之接口安全(一)----生成sign
在对于API的开发中 最让人头疼的 就是接口数据暴露 让一些有心之人 抓包之后恶意请求 那么如何解决这一弊端呢?自然而然的 我们就想到了 加密 那我们又如何加密 如何解密 才能使之有最安全的效率呢? ...
- FLUSH TABLES WITH READ LOCK 获取锁的速度
最近有一台MySQL的从库老是报延迟,观察到:FLUSH TABLES WITH READ LOCK,阻塞了4个多小时,还有另外一条SQL语句select *,从现象上来看是select * 阻塞了f ...
- 最简单之安装JDK
参考:https://www.cnblogs.com/lizhewei/p/11181082.html 1,百度搜索jdk 2,官网下载 jdk-8u161-linux-x64.rpm 或者jdk-8 ...
- Internet层协议下IP协议
Internet层协议特征 运行于 OSI 网络层面向无连接的协议独立处理数据包分层编址尽力而为传输无数据恢复功能 Internet层主要包含IP.ICMP.ARP.RARP几个协议. 这一主要说IP ...
- Hadoop-No.3之序列化存储格式
序列化存储指的是将数据结构转化为字节流的过程,一般用于数据存储或者网络传输.与之相反, 反序列化是将字节流转化为数据结果的过程.序列化是分布处理系统(比如Hadoop)的核心,原因在于他能对数据进行转 ...
- HDU-1358-Period(KMP, 循环节)
链接: https://vjudge.net/problem/HDU-1358#author=0 题意: For each prefix of a given string S with N char ...
- Mac修改显示器使支持原生缩放
教程 直接搬运没有意义,直接放链接.地址:https://bbs.feng.com/read-htm-tid-11677019.html 若无法访问请使用网页截图备份.地址:https://img20 ...
- es6的Set结构
今天看了一下es6的文档,发现还是比较实用的,Set结构可以用来数组的去重哎 let arr = [1,3,6,3,1,9] let arr1 = new Set(arr) [...arr1]的值就是 ...
- [Functional Programming] Church Encodings: Numberals
const log = console.log; // zero :: &fa.a const zero = f => x => x; // zero is F // once : ...