LWC 69: 774. Minimize Max Distance to Gas Station
LWC 69: 774. Minimize Max Distance to Gas Station
传送门:774. Minimize Max Distance to Gas Station
Problem:
On a horizontal number line, we have gas stations at positions stations[0], stations1, …, 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.
思路:
首先求出每个station之间的距离,考虑如下问题:两个station为[1, 9],gap为8。要插入一个station使得最大的最小,显然插入后应该为[1, 5, 9],最大间隔为4。举个反例,如果插入后为[1, 6, 9], [1, 3, 9],它们的最大间隔分别为5, 6,明显不是最小。从这里可以看出,对于插入k个station使得最大的最小的唯一办法是均分。
一种贪心的做法是,找到最大的gap,插入1个station,依此类推,但很遗憾,这种贪心策略是错误的。问题的难点在于我们无法确定到底哪两个station之间需要插入station,插入几个station也无法得知。
换个思路,如果我们假设知道了答案会怎么样?因为知道了最大间隔,所以如果目前的两个station之间的gap没有符合最大间隔的约束,那么我们就必须添加新的station来让它们符合最大间隔的约束,这样一来,对于每个gap我们是能够求得需要添加station的个数。如果需求数<=K,说明我们还可以进一步减小最大间隔,直到需求数>K。
Java版本:
public double minmaxGasDist(int[] stations, int K) {int n = stations.length;double[] gap = new double[n - 1];for (int i = 0; i < n - 1; ++i) {gap[i] = stations[i + 1] - stations[i];}double lf = 0;double rt = Integer.MAX_VALUE;double eps = 1e-7;while (Math.abs(rt - lf) > eps) {double mid = (lf + rt) /2;if (check(gap, mid, K)) {rt = mid;}else {lf = mid;}}return lf;}boolean check(double[] gap, double mid, int K) {int count = 0;for (int i = 0; i < gap.length; ++i) {count += (int)(gap[i] / mid);}return count <= K;}
Python版本:
class Solution(object):def minmaxGasDist(self, st, K):""":type stations: List[int]:type K: int:rtype: float"""lf = 1e-6rt = st[-1] - st[0]eps = 1e-7while rt - lf > eps:mid = (rt + lf) / 2cnt = 0for a, b in zip(st, st[1:]):cnt += (int)((b - a) / mid)if cnt <= K: rt = midelse: lf = midreturn rt
LWC 69: 774. Minimize Max Distance to Gas Station相关推荐
- Leetcode 774. Minimize Max Distance to Gas Station
LWC 69: 774. Minimize Max Distance to Gas Station 传送门:774. Minimize Max Distance to Gas Station Prob ...
- [Leetcode] 774. Minimize Max Distance to Gas Station 解题报告
题目: On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., st ...
- 加油python_力扣——gas station (加油站) python实现
题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- [leetcode] Gas Station
题目描述: There are N gas stations along a circular route, where the amount of gas at stationi is gas[i] ...
- PAT甲级1072 Gas Station (30 分):[C++题解]dijkstra算法、最短路
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 所有的dist[ ]都≤Ds:最小的dist[ ]最大; dist[ ] 总和最大. 由于加油站是字符,为了简单起见,将m个加油站编 ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- LeetCode 134 Gas Station
LeetCode 134 Gas Station 水题,暴力一下就ok class Solution { public:int tag[100005];int sum[100005];int canC ...
- LeetCode 134. Gas Station
LeetCode 134. Gas Station Solution1:我的答案,无数次试错得到的,不成系统,没有思路.. 时间复杂度O(n2)O(n2)O(n^2) class Solution { ...
最新文章
- 使用who.is查域名DNS信息以及用sameip.org查其他网站
- asp.net/c#字符格式化大总结
- 团队作业1(陈爽、夏江华、李瑞超、甘彩娈、吕乐乐)
- 参加51CTO学院软考培训,我通过啦!
- java与c应用,Java和C应用程序之间的IPC
- IBM存储部分常见配件PN号查询及描述翻译
- weblogic数据源配置的问题,weblogic密码破解
- ns-3文件编译出错总结
- 信息学奥赛一本通C++语言——1011: 甲流疫情死亡率
- 了不起!靠技术脱贫,他们只用了短短两年!
- 语音广播服务器,智慧校园语音广播说明(二)
- excel宏编程 c语言,宏(巨集)
- 美国2021年因极端天气损失上千亿美元
- 程序媛必备之日常BGM
- 我用wxPython搭建GUI量化系统之wx.grid实现excel功能
- 微信小程序的wxml、wxss、js、json的理解
- Linux下操作带空格的文件
- 阿里巴巴矢量库icon font的使用
- 提交 AjaxPro加载的速度
- uniapp引入阿里图标
热门文章
- 《大话数据结构》_程杰_学习笔记——第一章(java版本)
- 探索Headless Chrome
- linux--3--命令
- linux下如何运行.sh脚本
- IDEA 2019 激活码(注册码) -到20年有效
- 只能获取fixed语句初始值_VBA使用Sleep API暂停函数实现延时获取数据
- mysql数据库重装踩坑记
- multer 文件上传系统在express中的使用
- monit mysql_mysql – Monit服务名称错误
- C#修改connectionStrings的方法