Skip to content

Latest commit

 

History

History
99 lines (76 loc) · 2.91 KB

475-heaters.md

File metadata and controls

99 lines (76 loc) · 2.91 KB

475. Heaters - 供暖器

冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。

现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。

所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。

说明:

  1. 给出的房屋和供暖器的数目是非负数且不会超过 25000。
  2. 给出的房屋和供暖器的位置均是非负数且不会超过10^9。
  3. 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
  4. 所有供暖器都遵循你的半径标准,加热的半径也一样。

示例 1:

输入: [1,2,3],[2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。

示例 2:

输入: [1,2,3,4],[1,4]
输出: 1
解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。

题目标签:Binary Search

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
java 10 ms 40.9 MB
class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int[] heats = new int[heaters.length + 2];
        heats[0] = -0x3f3f3f3f;
        heats[heats.length - 1] = 2 * 0x3f3f3f3f;
        for (int i = 1; i < heats.length - 1; i++) {
            heats[i] = heaters[i - 1];
        }
        int res = 0;
        int left = 0, right = 1;
        for (int h : houses) {
            while (heats[right] <= h) {
                right++;
                left++;
            }
            res = Math.max(res, Math.min(h - heats[left], heats[right] - h));
        }
        return res;
    }
}
Language Runtime Memory
python3 124 ms 15.9 MB
class Solution:
    def findRadius(self, houses: List[int], heaters: List[int]) -> int:
        houses.sort()
        heaters.sort()
        heaters.insert(0, float('-inf'))
        heaters.append(float('inf'))
        p = 0
        q = 1
        res = 0
        while p < len(houses) and q < len(heaters):
            if houses[p] < heaters[q]:
                res = max(res, min(heaters[q] - houses[p], houses[p] - heaters[q - 1]))
                p += 1
            else:
                q += 1
        return res