[Leetcode] Binary search -- 475. Heaters

时间:2022-10-11 23:44:51

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. &lbrack;LeetCode&rsqb; 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 &amp&semi; Binary Search 解题模版

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

  4. &lbrack;LeetCode&rsqb; 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&period; Find Minimum in Rotated Sorted Array&lpar;leetcode&comma; binary search&rpar;

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

  7. &lbrack;Leetcode&rsqb; Binary search&comma; Divide and conquer--240&period; 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. &lbrack;Leetcode&rsqb; Binary search&comma; DP--300&period; Longest Increasing Subsequence

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

  9. LeetCode&colon; Binary Search Tree Iterator 解题报告

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

随机推荐

  1. HYPER V 文件共享 复制文件 共享硬盘 来宾服务

    虚拟机的设置   -->  集成服务 –> 来宾服务  勾选    文件就可以在本地机器和虚拟机上来回复制了. 他可让 Hyper-V 管理员在运行虚拟机的同时将文件复制到虚拟机,且无需使 ...

  2. Scala class的构造方法与继承

    有java背景的人,很清楚java是如何定义构造方法以及继承的.在scala里面,继承和java有些相似.但是构造方法的定义,就不大一样了,应该说是差别还是很大的.在java里面,定义构造方法,就是定 ...

  3. Show in Finder OC代码

    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:urls]; /* Activate the Finder, and op ...

  4. final运用于内部类访问局部变量

    final运用于内部类访问局部变量 public void mRun( final String name){ new Runnable() { @Override public void run() ...

  5. DedeCMS Error&colon;Tag disabled&colon;&quot&semi;php&quot&semi;的解决办法

  6. 浏览器加载和渲染html的顺序-css渲染效率的探究

    1.浏览器加载和渲染html的顺序1.IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的.2.在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素都 ...

  7. C&num;连接sqlserver数据库

    // 混合登录 写法1:Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPas ...

  8. 51nod 1770 数数字

    1770 数数字 基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题  收藏  关注 统计一下 aaa ⋯ aaan个a × b 的结果里面 ...

  9. Linux Namespace &colon; Network

    Network namespace 在逻辑上是网络堆栈的一个副本,它有自己的路由.防火墙规则和网络设备.默认情况下,子进程继承其父进程的 network namespace.也就是说,如果不显式创建新 ...

  10. spring&plus;redis的集成,redis做缓存

    1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...