Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.

Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.

So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.

Note:
Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
As long as a house is in the heaters' warm radius range, it can be warmed.
All the heaters follow your radius standard and the warm radius will the same.
Example 1:
Input: [1,2,3],[2]
Output: 1
Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
Example 2:
Input: [1,2,3,4],[1,4]
Output: 1
Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.

本题属于Easy级别,最常规的方法是用两层循环遍历houses和heaters数组,得到每个house与所有heaters的距离的最小值,然后取出所有最小值中的最大值即为radius。但是这种解法时间复杂度是O(n^2),会导致Time Exceed Limited。

仔细想想,其实根本不需要每个house和所有的heaters进行比较,只需要比较一左一右相邻的两个heaters即可。如何快速找到两个相邻的heaters,可以先对heaters进行排序,再利用二分查找的原理。

参考代码如下:

class Solution(object):
def binSearch(self,l,n):
low = 0
high = len(l)-1
while low < high:
mid = (low + high)/2
if n == l[mid]:
return 0
elif n < l[mid]:
if mid - 1 >= 0 and n >= l[mid-1] :
return min(abs(n-l[mid]),abs(n-l[mid-1]))
else:
high = mid - 1
elif n > l[mid]:
if mid + 1 <= len(l)-1 and n <= l[mid +1]:
return min(abs(n-l[mid]),abs(n-l[mid+1]))
else:
low = mid + 1
return abs(n - l[low])
def findRadius(self, houses, heaters):
"""
:type houses: List[int]
:type heaters: List[int]
:rtype: int
"""
radius = 0
heaters.sort()
for i in houses:
minDis = self.binSearch(heaters, i)
print minDis
if minDis != -1 and radius < minDis:
radius = minDis
return radius

【leetcode】Heaters的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. 关于Anaconda3 (64-bit)的一些体验

    最近因为在学习数据分析,所以安装了Anaconda3 (64-bit),最新版,支持py3.7 优点:自带了720个库(官方宣布),自带notebook,spyder.不用自己再去pip各种库了(基本 ...

  2. P1936 【水晶灯火灵】

    lalala~~(才不会告诉你这是题面呢) 这题确实有点坑,第一遍穷举超时,然后就开始了漫漫找规律之路... 终于,在经过5分钟的纠结之后,我终于发现了这个神奇的规律,那就是 Fabonacci!!! ...

  3. openvswitch安装与使用

    wget http://openvswitch.org/releases/openvswitch-2.4.1.tar.gz tar -xvf openvswitch-2.4.1.tar.gz cd o ...

  4. SSCursor 处理大量数据

    使用游标的好处是不会将查询结果全部都放入内存中,避免了占用大量的内存,会从存储块中读取记录,并且一条一条的返回来 class DbConnection(object): def __init__(se ...

  5. mongodb增删改查常用命令总结

    前言 去年我还折腾过mongodb,后来用不到也就没碰了,这就导致了我忘的一干二净,不得不感叹,编程这东西只要不用,就会忘没了.现在我想重拾mongodb,来总结一下常用命令,主要就是增删改查. 另外 ...

  6. java_第一年_JDBC(1)

    JDBC(Java Data Base Connectivity),用于实现java语言编程与数据库连接的API. 数据库驱动:应用程序并不能直接使用数据库,而需要通过相应的数据库驱动程序后再操作数据 ...

  7. noip2015day2-运输计划

    题目描述 公元$ 2044 $年,人类进入了宇宙纪元. \(L\) 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个星球之间,这 \(n-1\) 条航道连通了 \(L\) ...

  8. laravel修改用户模块的密码验证

    做项目的时候,用户认证几乎是必不可少的,如果我们的项目由于一些原因不得不使用 users 之外的用户表进行认证,那么就需要多做一点工作来完成这个功能. 现在假设我们只需要修改登录用户的表,表名和表结构 ...

  9. Pycharm Debug 问题

    Pycharm debug 出现如下问题 Connected to pydev debugger (build 181.4668.75) Traceback (most recent call las ...

  10. AtCoder Beginner Contest 072

    这应该是我第二次打AtCoder, 题目其实并不难,就是自己经验不足想复杂了,再加上自己很笨,愣是做了97分钟才全做出来(最后三分钟,有点小激动..),看着前面大牛半个小时都搞完了,真心膜拜一下,代码 ...