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:

  1.  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的更多相关文章

  1. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  2. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

  3. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  4. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  6. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

  7. [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 ...

  8. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

随机推荐

  1. iOS 2017年, 上传审核被拒绝.到奔溃

    2017年,苹果并没有因为新年的气氛而对CP们"网开一面".频繁锁榜.调整排名规则以及关键词覆盖算法--不断抛出的大动作,让CP们叫苦不迭.且从1月初开始,苹果还进一步加强了对应用 ...

  2. 多个git账号的SSH配置

    一般使用git都只需要维持一个默认的git账户就可以打天下了. 但如果自己确实需要多个git账号的需求的话,就有必要配置多个ssh key了. 首先为生成多个ssh key ssh-keygen -t ...

  3. UE4 Fade out Mesh

    由于项目需要一个将场景慢慢淡入以及淡出的效果,所以就想了想实现思路.因为PBR光照模型是不支持透明物体的渲染的,所以UE4中的PBR材质在为Opaque时是无法改变透明度的,想来想去想不出解决方法,然 ...

  4. .net Framework 4.5 新特性async(异步)的初步认识

    1.async的简单说明 继版本4.5以前,要想实现异步方法,运用多线程齐头并进.而4.5直接一个async修饰的方法配合await实现异步,这里的底层实现原理暂时未研究, 应该本质都一样,对线程的操 ...

  5. Spring+SpringMVC+Mybaties整合之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/27

    以下配置可直接使用,只需更改包名. 关于内部标签的解释及用法,都以注解形式在代码内部说明.个人原创,转载需注明出处. 1,web.xml.添加jar包后首先需要配置WEB-INF下的web.xml文件 ...

  6. lighttpd启动问题

    /home/yuna/web/app/lighttpd/sbin/lighttpd -f /home/yuna/web/app/lighttpd/lighttpd.conf -m /home/yuna ...

  7. curl_escape ---> 使用URL 编码给定的字符串

    curl_escape (PHP 5 >= 5.5.0) curl_escape — 使用 URL 编码给定的字符串 说明¶ string curl_escape ( resource $ch  ...

  8. caffe:使用C++来提取任意一张图片的特征(从内存读取数据)

    0x00 关于使用C++接口来提取特征,caffe官方提供了一个extract_features.cpp的例程,但是这个文件的输入是blob数据,即使输入层使用的是ImageData,也需要在depl ...

  9. bootstrap的常用组件和栅格式布局

    Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. 因为Bootstrap需要jquery的支持,所以在使用Bootstrap之前要 ...

  10. webService 下得 拦截

    当我们 对webservice 接口的 拦截 更具权限  来 判断 是否可以调用  一下是我的 一个demo 首先 我们写一个 拦截类 import javax.xml.soap.SOAPExcept ...