LeetCode-475 Heaters

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 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 warm

2 pointer solution

Trick here is to sort both arrays, and keep the j index going on, because the distance from one house to a heater will only increases when the minimum has already been found, plus that i increases when j increases. So no need to go back and make this O(n^2), which will TLE.

And, we should think from the perspective of houses, not heaters. The test case has the situation that the heaters number doesn’t show in the houses array.

class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        int max = 0, i=0, j=0;
        Arrays.sort(houses);
        Arrays.sort(heaters);

        while(i<houses.length){
            int dist = Integer.MAX_VALUE;
            while(j<heaters.length-1){
                if(Math.abs(heaters[j+1]-houses[i])>Math.abs(heaters[j]-houses[i])) break;
                j++;
            }

            dist=Math.abs(heaters[j]-houses[i]);
            max=Math.max(max,dist);
            i++;
        }
        return max;

    }
}

Binary search solution:

The idea is to leverage decent Arrays.binarySearch() function provided by Java.

public static int binarySearch(data_type arr, data_type key )

Arrays.binarySearch() 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.
It requires the array to be sorted, or else the return value is undefined.

For each house, find its position between those heaters (thus we need the heaters array to be sorted).
Calculate the distances between this house and left heater and right heater, get a MIN value of those two values. Corner cases are there is no left or right heater.
Get MAX value among distances in step 2. It’s the answer.
Time complexity: max(O(nlogn), O(mlogn)) - m is the length of houses, n is the length of heaters.

public class Solution {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(heaters);
        int result = Integer.MIN_VALUE;

        for (int house : houses) {
            int index = Arrays.binarySearch(heaters, house);
            if (index < 0) {
            index = -(index + 1);
            }
            int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
            int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;

            result = Math.max(result, Math.min(dist1, dist2));
        }

        return result;
    }
}