[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 ...
随机推荐
- JS中的循环嵌套 BOM函数
[嵌套循环特点] 外层循环转一次,内层循环转一圈 外层循环控制行数,内层循环控制每行元素个数 [做 ...
- PHP 魔术方法__set() __get() 方法
a); //output: 123 var_dump($s->b); //output: 123 var_dump($s->c); //output: null var_dump($s-& ...
- 如何给远程主机开启mysql远程登录权限
# 如何给远程主机开启mysql远程登录权限 > 在千锋学习PHP的有些学员会在阿里或者腾讯云去购买自己的云服务器.在初级阶段的项目上线时会遇到一个问题,就是无法使用远程连接工具操作自己线上的m ...
- WPF触屏Touch事件在嵌套控件中的响应问题
前几天遇到个touch事件的坑,记录下来以增强理解. 具体是 想把一个listview嵌套到另一个listview,这时候如果list view(子listview)的内容过多超过容器高度,它是不会出 ...
- 关于Integer与int
integer a=new integer(1); integer b=new integer(1); int c=1; integer d=1; a==b false因为地址不同: a==c t ...
- 【Vue 入门】使用 Vue2 开发一个展示项目列表的应用
前言 一直没有找到一个合适的展示个人项目的模板,所以自己动手使用 Vue 写了一个.该模板基于 Markdown 文件进行配置,只需要按一定规则编写 Markdown 文件,然后使用一个 在线工具 转 ...
- 在linux系统中跟踪高IO等待
原文作者:Jon Buys 原文地址:http://ostatic.com/blog/tracking-down-high-io-wait-in-linux 译者:Younger Liu,本作品采用知 ...
- java并发程序——Excutor
概述 Excutor这个接口用的不多,但是ThreadPoolExcutor这个就用的比较多了,ThreadPoolExcutor是Excutor的一个实现.Excutor体系难点没有,大部分的关键点 ...
- How to image a CD/DVD ROM and generate hash value
Someone ask me how to image a CD/DVD ROM and generate hash value in the same time. A small tool call ...
- 学习笔记:JavaScript-进阶篇
1.二维数组 二维数组的表示: myarray[ ][ ] var myarr=new Array(); //先声明一维 for(var i=0;i<2;i++){ //一维长度为2 ...