LeetCode #475 Heaters 供暖器

475 Heaters 供暖器

Description:
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:

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

Example:

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 1 standard, then all the houses can be warmed.

題目描述:
冬季已經(jīng)來臨。 你的任務是設計一個有固定加熱半徑的供暖器向所有房屋供暖。

現(xiàn)在,給出位于一條水平線上的房屋和供暖器的位置,找到可以覆蓋所有房屋的最小加熱半徑。

所以,你的輸入將會是房屋和供暖器的位置。你將輸出供暖器的最小加熱半徑。

說明:

給出的房屋和供暖器的數(shù)目是非負數(shù)且不會超過 25000。
給出的房屋和供暖器的位置均是非負數(shù)且不會超過10^9。
只要房屋位于供暖器的半徑內(nèi)(包括在邊緣上),它就可以得到供暖。
所有供暖器都遵循你的半徑標準,加熱的半徑也一樣。

示例 :

示例 1:

輸入: [1,2,3],[2]
輸出: 1
解釋: 僅在位置2上有一個供暖器。如果我們將加熱半徑設為1,那么所有房屋就都能得到供暖。

示例 2:

輸入: [1,2,3,4],[1,4]
輸出: 1
解釋: 在位置1, 4上有兩個供暖器。我們需要將加熱半徑設為1,這樣所有房屋就都能得到供暖。

思路:

題意是找到離房子最近的加熱器的距離的最大值

  1. 對加熱器數(shù)組進行排序, 遍歷房子數(shù)組, 用二分查找找到離房子最近的加熱器, 更新房子和加熱器的距離
    時間復雜度O(mlgm), 空間復雜度O(1)
  2. 對兩個數(shù)組都進行排序
    時間復雜度O(mn), 空間復雜度O(1)

代碼:
C++:

class Solution 
{
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) 
    {
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        // vector<int>::iterator -> auto
        vector<int>::iterator i = heaters.begin(), j = houses.begin();
        int result = 0;
        while (j != houses.end()) 
        {
            while (i != heaters.end() - 1 and abs(*(i + 1) - *j) <= abs(*i - *j)) i++;
            result = max(result, abs(*i - *j));
            j++;
        }
        return result;
    }
};

Java:

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        int result = 0;
        Arrays.sort(heaters);
        for (int house : houses) result = Math.abs(binarySearch(heaters, house) - house) > result ? Math.abs(binarySearch(heaters, house) - house) : result;
        return result;
    }

    private int binarySearch(int[] heaters, int target) {
        int left = 0, right = heaters.length - 1;
        while (left < right - 1) {
            int mid = left + ((right - left) >> 1);
            if (target == heaters[mid]) return heaters[mid];
            else if (target < heaters[mid]) right = mid;
            else left = mid;
        }
        if (Math.abs(target - heaters[left]) < Math.abs(target - heaters[right])) return heaters[left];
        return heaters[right];
    }
}

Python:

class Solution:
    def findRadius(self, houses: List[int], heaters: List[int]) -> int:
        result = 0
        heaters.sort()
        for house in houses:
            result = max(abs(self.binary_search(heaters, house) - house), result)
        return result

    def binary_search(self, heaters: List[int], target: int) -> int:
        left, right = 0, len(heaters) - 1
        while left < right - 1:
            mid = ((right - left) >> 1) + left
            if target == heaters[mid]:
                return heaters[mid]
            elif target < heaters[mid]:
                right = mid
            else:
                left = mid
        if abs(target - heaters[left]) < abs(target - heaters[right]):
            return heaters[left]
        return heaters[right]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容