Leetcode 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。
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
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], ..., st ...
- 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 Prob ...
- 加油python_力扣——gas station (加油站) python实现
题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[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 { ...
- LeetCode 134.Gas Station 解题分析
题目来源: https://leetcode.com/problems/gas-station/description/ 题目描述: There are N gas stations along a ...
- [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)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- 【LeetCode】849. Maximize Distance to Closest Person
[LeetCode]849. Maximize Distance to Closest Person 849. Maximize Distance to Closest Person Easy In ...
最新文章
- California Dreaming
- 大脑只需单个神经元就可进行XOR异或运算,Science新研究揭开冰山一角,引发热议...
- SE11 仅以有限的方式支持生成的 DDL SQL 视图
- 洛谷 - P4197 Peaks(Kruskal重构树+dfs序+主席树)
- aspxgridview 增加行号
- WD硬盘 C1门 解决办法
- python2中xrange比range优点_【Python面试】 说说Python中xrange和range的区别?
- 液晶显示模块的分类和基本知识
- 【金融申请评分卡】目标变量界定
- MODIS数据的简介和下载(一)——MODIS数据简介
- css背景颜色跟随文字颜色、设置文字颜色反色
- 区块链在保险业的应用
- html alert字体颜色,js里alert里的字体颜色怎么设置:字体颜色方法;fontcolor(color)...
- GitHub 上排名前 100 的 Android 开源库介绍
- 电话机器人是如何筛选意向客户的
- 这是一个怎样的零售行业数字化运营方案?让多家公司实现营收翻倍!
- 非易失性内存技术及数据库
- LINUX 管理 SAMBA 用户密码命令详解
- JS获取网页 < 变成 /u003C
- 关于V5.7 正式版 自定义字段图片上传无法点击选择的问题解决!
热门文章
- 智能硬件PICOOC:没有金刚钻,怎么揽得住烫手的2100万?
- emlog链接html,实现emlog的友情链接只在首页显示
- mongodb删除重复数据保留一条
- 【40张linux思维导图】
- ESP32-CAM上手第一步——资料不能少之我的手记
- java中char与int的转换问题
- oracle安装图解
- Linux循环中累计数据,linux shell 读取for循环中出现难处理的数据之单引号错误实例...
- 简单工厂(Simple Factory)
- JavaWeb_(SSH论坛)_七、辅助模块