[Leetcode] Binary search -- 475. 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.
https://leetcode.com/problems/heaters/#/description
===========
Solution:
- 1st try, naive method. TLE using python.
The idea is to try iterate every house in houses hs, then we use hs to compare with each heater and get smallest distance "dist". So we have distance sets "dists"
find the maximum distance s from dists as the final answer, which is the minimum radius standard of heaters.
time complexity o(m*n), where m = len(houses), n = len(heaters)
ansMax = 0
houses.sort()
heaters.sort()
for hs in houses:
minDiff = 2**31 #get min
for ht in heaters:
if abs( hs -ht) < minDiff:
minDiff = abs(ht -hs) if minDiff > ansMax:
ansMax = minDiff
return ansMax
2. Use binary search. The idea is similar as above. But it finds the left index of the most closest smaller(equal) ele, the right index closest bigger (equal) ele; then we compare to get the smallest distance "dist" for each position ; ( the function same as the bisect algorithm in python) , then we find the maximum distance s from dists as the final answer, which is the minimum radius standard of heaters.
Time complexity is o(mlogm+nlogn + m*lgn)
def binarySearch(lst, ele):
if len(lst) == 1:
return (0, 0)
l = 0
h = len(lst) - 1
while (l <= h):
mid = (l+h)/2
if lst[mid] == ele:
return (mid, mid)
elif lst[mid] < ele:
l = mid + 1
else:
h = mid - 1
return (l-1, l) #return left, right index houses.sort()
heaters.sort()
ansMax = 0
for hs in houses:
(l, r) = binarySearch(heaters, hs)
#print('l, r: ', l,r )
if r > len(heaters)-1: #only left is valid
minDiff = abs(hs- heaters[l])
elif l < 0:
minDiff = abs(heaters[r] - hs)
else:
minDiff = min(abs(hs - heaters[l]), abs(heaters[r] -hs))
#print('lminDi: ', minDiff )
if minDiff > ansMax:
ansMax = minDiff
return ansMax
[Leetcode] Binary search -- 475. Heaters的更多相关文章
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
- LeetCode & Binary Search 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- [Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
随机推荐
- Magento中URL路径的获取
//获得 media 带 http 的url 地址. Mage::getBaseUrl('media') //获得skin 和js 目录的地址: Mage::getBaseUrl('skin'); M ...
- JQuery使用mousedown和mouseup简单判断鼠标按下与释放位置是否相同
在JQuery中,我们可以利用mousedown.mouseup来跟踪页面的鼠标按下与释放事件. 如何获取鼠标的位置信息呢?事件event的pageX和pageY属性可以让我们获得鼠标在页面中的具体位 ...
- hdu1054最小顶点覆盖
最小定点覆盖是指这样一种情况: 图G的顶点覆盖是一个顶点集合V,使得G中的每一条边都接触V中的至少一个顶点.我们称集合V覆盖了G的边.最小顶点覆盖是用最少的顶点来覆盖所有的边.顶点覆盖数是最小顶点覆盖 ...
- Bootstrap之折叠(Collapse)插件
学习资料:Bootstrap折叠(Collapse)插件 大家可能常见的都是类似: 这种的效果,小颖今天要给大家分享一个不一样的效果嘻嘻.铛铛铛铛........................... ...
- ABP文档 - 对象与对象之间的映射
文档目录 本节内容: 简介 IObjectMapper 接口 集成 AutoMapper 安装 创建映射 自动映射的特性 自定义映射 扩展方法 MapTo 单元测试 预定义的映射 Localizabl ...
- FAILED: SemanticException [Error 10096]: Dynamic partition strict mode
因为动态分区只能在非严格模式进行... 修改为非严格模式后即可! set hive.exec.dynamic.partition.mode
- hdu4463 Outlets 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4463 很裸的一道题目,稍微处理一下输入即可 代码: #include<iostream> ...
- HAProxy的三种不同类型配置方案
haproxy是一款功能强大.灵活好用反向代理软件,提供了高可用.负载均衡.后端服务器代理的功能,它在7层负载均衡方面的功能很强大(支持 cookie track, header rewrite等等) ...
- Advanced Sort Algorithms
1. Merge Sort public class Mergesort { private int[] numbers; private int[] helper; private int numb ...
- Aspose.Cells.dll操作execl
附件:Aspose.Cells.dll 1.创建execl(不需要服务器或者客户端安装office) public void DCExexl(DataTable dt) { Workbook wb ...