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. mybatis 动态SQL .2

    目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,otherwise) 语句 5.动态SQL:tri ...

  2. JSON中文处理类实例

    $array = array( 'Name'=>'络恩', 'Age'=>24); $post=my_json_encode($array); // 这个函数是判断版本,如果是搞版本的则直 ...

  3. selenium:css_selector定位详解

    selenium:css_selector定位详解(css selector和xpath的比较) 来源:https://www.cnblogs.com/haifeima/p/10138154.html ...

  4. mysql——多表——内连接查询

    内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表: 当该字段的值相等时,就查询出该记录. 前期准备两个表: ), d_id ), name ) ...

  5. C语言博课作业11

    一.本周作业头 这个作业属与那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/10130 ...

  6. AKKA学习(二) 未完

    Actor调用 从上面的例子中,我们可以大概的对AKKA在JAVA中的使用有一个全局的概念.这里我们在稍微细致的讲解一下. 在JAVA中使用AKKA进行开发主要有这几个步骤: 定义消息模型. 创建Ac ...

  7. 洛谷P1029 最大公约数和最小公倍数问题 (简单数学题)

    一直懒的写博客,直到感觉不写不总结没有半点进步,最后快乐(逼着)自己来记录蒟蒻被学弟学妹打压这一年吧... 题目描述 输入22个正整数x_0,y_0(2 \le x_0<100000,2 \le ...

  8. c++ Convert struct to bytes

    D:\stock\Tskingfromgoogle\src\NetTS\TW.cpp Convert struct  to bytes //Convert struct to bytes 2019/0 ...

  9. C语言---进制

    1. 何为进制 进位机制,逢几进一.数值某一位置上的数在运算时是逢几进一. 生活中的进制:十进制.十二进制(12个月是1年).六十进制(60秒是1分钟) 计算机编程中的进制:二进制.八进制.十六进制. ...

  10. ftp读取图片并转Base64

    public String download(String ftpUrl,String sfzh){ FTPClient ftpClient = new FTPClient(); InputStrea ...