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:

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

Solution

Two-pointer, time O(nlogn), space O(1)

对于每个House,找到离他最近的Heater,并统计Radius。

这里有一个坑,将
Math.abs(heaters[j] - house) >= Math.abs(heaters[j + 1] - house)
改成
Math.abs(heaters[j] - house) > Math.abs(heaters[j + 1] - house)
会有错误,原因未知。

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        if (houses == null || heaters == null || houses.length < 1 || heaters.length < 1) {
            return 0;
        }
        
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int radius = 0;
        int j = 0;

        for (int house : houses) {
            while (j < heaters.length - 1 
                  && Math.abs(heaters[j] - house) 
                        >= Math.abs(heaters[j + 1] - house)) {
                ++j;
            }
            
            radius = Math.max(radius, Math.abs(heaters[j] - house));
        }
                              
        return radius;
    }
}

Binary search, time O(max(m, n) * logm)

利用了Arrays.binarySearch API:

public static int binarySearch(int[] a, int key)

Returns:
index of the search key, if it is contained in the array;
otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key.
Note that this guarantees that the return value will be >= 0 if and only if the key is found.

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(heaters);
        int radius = 0;
        
        for (int house : houses) {
            int i = Arrays.binarySearch(heaters, house);
            if (i < 0) {
                i = -(i + 1);
            }
            
            int radius1 = i > 0 ? Math.abs(heaters[i - 1] - house) 
                : Integer.MAX_VALUE;
            int radius2 = i < heaters.length ? Math.abs(heaters[i] - house) 
                : Integer.MAX_VALUE;
            radius = Math.max(radius, Math.min(radius1, radius2));
        }
        
        return radius;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容