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相关推荐

  1. [Leetcode] 774. Minimize Max Distance to Gas Station 解题报告

    题目: On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., st ...

  2. 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 ...

  3. 加油python_力扣——gas station (加油站) python实现

    题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...

  4. LeetCode 134 Gas Station

    LeetCode 134 Gas Station 水题,暴力一下就ok class Solution { public:int tag[100005];int sum[100005];int canC ...

  5. LeetCode 134. Gas Station

    LeetCode 134. Gas Station Solution1:我的答案,无数次试错得到的,不成系统,没有思路.. 时间复杂度O(n2)O(n2)O(n^2) class Solution { ...

  6. LeetCode 134.Gas Station 解题分析

    题目来源: https://leetcode.com/problems/gas-station/description/ 题目描述: There are N gas stations along a ...

  7. [leetcode] Gas Station

    题目描述: There are N gas stations along a circular route, where the amount of gas at stationi is gas[i] ...

  8. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  9. 【LeetCode】849. Maximize Distance to Closest Person

    [LeetCode]849. Maximize Distance to Closest Person 849. Maximize Distance to Closest Person Easy In ...

最新文章

  1. California Dreaming
  2. 大脑只需单个神经元就可进行XOR异或运算,Science新研究揭开冰山一角,引发热议...
  3. SE11 仅以有限的方式支持生成的 DDL SQL 视图
  4. 洛谷 - P4197 Peaks(Kruskal重构树+dfs序+主席树)
  5. aspxgridview 增加行号
  6. WD硬盘 C1门 解决办法
  7. python2中xrange比range优点_【Python面试】 说说Python中xrange和range的区别?
  8. 液晶显示模块的分类和基本知识
  9. 【金融申请评分卡】目标变量界定
  10. MODIS数据的简介和下载(一)——MODIS数据简介
  11. css背景颜色跟随文字颜色、设置文字颜色反色
  12. 区块链在保险业的应用
  13. html alert字体颜色,js里alert里的字体颜色怎么设置:字体颜色方法;fontcolor(color)...
  14. GitHub 上排名前 100 的 Android 开源库介绍
  15. 电话机器人是如何筛选意向客户的
  16. 这是一个怎样的零售行业数字化运营方案?让多家公司实现营收翻倍!
  17. 非易失性内存技术及数据库
  18. LINUX 管理 SAMBA 用户密码命令详解
  19. JS获取网页 < 变成 /u003C
  20. 关于V5.7 正式版 自定义字段图片上传无法点击选择的问题解决!

热门文章

  1. 智能硬件PICOOC:没有金刚钻,怎么揽得住烫手的2100万?
  2. emlog链接html,实现emlog的友情链接只在首页显示
  3. mongodb删除重复数据保留一条
  4. 【40张linux思维导图】
  5. ESP32-CAM上手第一步——资料不能少之我的手记
  6. java中char与int的转换问题
  7. oracle安装图解
  8. Linux循环中累计数据,linux shell 读取for循环中出现难处理的数据之单引号错误实例...
  9. 简单工厂(Simple Factory)
  10. JavaWeb_(SSH论坛)_七、辅助模块