【leetcode】Heaters
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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- Sublime Text 3 相关
Sublime Text 3 相关 Sublime Text 3是款非常实用代码编辑神器,但是想要用任何一款软件,掌握一些快捷键还是很有必要的.. 将Sublime Text 3 添加到右键选项中 打 ...
- Mac_Navicat Premium连接MySQL错误2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_sha2_password.so, 2): image not found
mac下MySql启动连接报错:Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(/usr/local/my ...
- CF-Div.3-B. Minimize the Permutation【模拟·需要清醒的脑子】
题目传送门 根据字典序,是个人都会想到依次把目前最小的数尽量往前面移动,直到它不能再往前移动,或者已经到了它的期望位置(就是排列的那个位置 比如$i$就应该在位置$i$)为止. 所以我刚开始是这么写的 ...
- 图片下载&&非同源图片下载&&同源下载&&网页点击下载图片
非同源图片下载(html添加canvas标签) 方法1: downloadImgByBase64(url){ console.log() // 创建隐藏的可下载链接 // let blob = ...
- c++ xml 解析“后直接跟值问题
c++ xml库相关 要解析内容: <ITEM name="SLSJ"head="SLSJ"/> 代码: GetNodeAttri(subnodes ...
- 普通帐号起redis
wget http://download.redis.io/releases/redis-4.0.11.tar.gz $ tar xzf redis-4.0.11.tar.gzmv redis-4.0 ...
- [转帖]「白帽黑客成长记」Windows提权基本原理(上)
「白帽黑客成长记」Windows提权基本原理(上) https://www.cnblogs.com/ichunqiu/p/10949592.html 我们通常认为配置得当的Windows是安全的,事实 ...
- MySql数据库优化-汇总
各位,不喜勿喷,和气生财- 数据库优化,是一种综合性的技术,不是通过某一种方式让数据库效率提高很多,而是通过各个方面的优化,来是数据库效率明显的稳步的提高. 主要包括以下: 1.库表的设计优化(三种范 ...
- 浅谈React工作原理
浅谈React工作原理:https://www.cnblogs.com/yikuu/p/9660932.html 转自:https://cloud.tencent.com/info/63f656e0b ...
- Oracle数据库跟踪存储过程
1.[右击]该存储过程 ——>勾选[添加调试信息]——>点击[测试]选项 2.输入对应参数,点击下图左上角红框按钮,开始跟踪,可逐语句逐过程跟踪