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. 读书笔记之JavaScript中的数据类型(1)

    JavaScript严格意义上分为ECMAScript.DOM.BOM.ECMAScript是一门真正意义上的语言,独立于浏览器,浏览器只是它的一个宿主环境.DOM(文档对象模型),为ECMAScri ...

  2. Python全栈之路-Day31

    1 反射 反射的精髓是通过字符串去获取对象属性的值 1.1 基于类和对象反射的属性 #!/usr/bin/env python # __Author__: "wanyongzhen" ...

  3. 【代码学习】MYSQL数据库的常见操作

    ---恢复内容开始--- ============================== MYSQL数据库的常见操作 ============================== 一.mysql的连接与 ...

  4. Composer 常用命令总结(三)

    init(初始化) 该命令用于创建 composer.json 文件,并进行基础信息配置: $ composer init 可以配置Package name.Description.Author.Mi ...

  5. UML总结(对九种图的认识和如何使用Rational Rose 画图)

    UML是一种建模语言,是系统建模的标准.我们之所以建模是因为大规模的系统设计时相当复杂的,当系统比较复杂时就会涉及到以下这几个问题: 开发人员如何与用户进行沟通来了解系统的需求? 开发人员之间如何沟通 ...

  6. IOS的Application以及IOS目录的介绍

    1.UIApplication介绍 .UIApplication对象是应用程序的象征 .每一个应用都有自己的UIApplication对象,而且是单例的 .通过[UIApplication share ...

  7. JavaScript ,Python,java,C#,Go系列算法之【插入排序篇】

    常见的内部排序算法有:插入排序.希尔排序.选择排序.冒泡排序.归并排序.快速排序.堆排序.基数排序等.用一张图概括: 插入排序 插入排序(英语:Insertion Sort)是一种简单直观的排序算法. ...

  8. 《安卓网络编程》之第一篇 java环境下模拟客户端、服务器端

    1.Socket简介 在网络上的两个程序通过一个双向的通信连接实现数据的交换,这个双向链路的一端称为一个Socket.Socket通常用来实现客户方和服务方的连接.Socket是TCP/IP协议的一个 ...

  9. 关于RFID2.4G 标签卡最新方案

    它是一款针对RFID有源卡行业设计的,是一款单向的2.4G频段RF射频芯片,目前主要针对低功耗的校讯通, 2.4G停车场,电动车防盗, 闪光灯设备(引闪器) ,智能家居等领域.SI24R2E 同样与S ...

  10. 利用R语言进行交互数据可视化(转)

    上周在中国R语言大会北京会场上,给大家分享了如何利用R语言交互数据可视化.现场同学对这块内容颇有兴趣,故今天把一些常用的交互可视化的R包搬出来与大家分享. rCharts包 说起R语言的交互包,第一个 ...