[LeetCode]Heaters

题目描述:

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

Note:

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  3. As long as a house is in the heaters' warm radius range, it can be warmed.
  4. All the heaters follow your radius standard and the warm radius will the same.

Example 1:

Input: [1,2,3],[2]

Output: 1

Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.

Example 2:

Input: [1,2,3,4],[1,4]

Output: 1

Explanation: The two heater was placed in the position 1 and 4. We need to use radius 2 standard, then all the houses can be warmed.

题目大意:

冬天要来了!今天比赛的第一个任务就是设计加热器温暖房屋。

给定一组排列在水平线上的房屋和加热器,计算最小的加热器半径,使得所有房屋都可以被覆盖。

输入房屋和加热器的坐标,输出期望的加热器半径的最小值。

注意:

  1. 房屋和加热器的数目均为非负整数,并且不大于25000
  2. 房屋和加热器的位置是非负整数,并且不超过10^9
  3. 位于加热器半径范围内的房屋均可被加热
  4. 加热器的半径都是一样的

解题思路:

排序(Sort) + 二分查找(Binary Search)

升序排列加热器的坐标heaters

遍历房屋houses,记当前房屋坐标为house:

    利用二分查找,分别找到不大于house的最大加热器坐标left,以及不小于house的最小加热器坐标right
    
    则当前房屋所需的最小加热器半径radius = min(house - left, right - house)
    
    利用radius更新最终答案ans

Python代码:

class Solution(object):
    def findRadius(self, houses, heaters):
        """
        :type houses: List[int]
        :type heaters: List[int]
        :rtype: int
        """
        ans = 0
        heaters.sort()
        for house in houses:
            radius = 0x7FFFFFFF
            le = bisect.bisect_right(heaters, house)
            if le > 0:
                radius = min(radius, house - heaters[le - 1])
            ge = bisect.bisect_left(heaters, house)
            if ge < len(heaters):
                radius = min(radius, heaters[ge] - house)
            ans = max(ans, radius)
        return ans

 

本文链接:http://bookshadow.com/weblog/2016/12/11/leetcode-heaters/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

暂无评论

张贴您的评论