One pass Java clean solution


  • 0
    S
    public int findRadius(int[] houses, int[] heaters) {
            Arrays.sort(houses);
            Arrays.sort(heaters);
            int hlen = heaters.length;
    	int h = 0, res = 0, rad;
    	for (int i = 0; i < houses.length; i++) {
    		while (h+1<hlen && houses[i]>heaters[h+1]) h++;
    		if (h+1<hlen) rad = Math.min(Math.abs(houses[i]-heaters[h]), Math.abs(houses[i]-heaters[h+1]));
    		else rad = Math.abs(houses[i]-heaters[h]);
    		if (rad>res) res = rad;
    	}
    	return res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.