Java easy solution


  • 0
    P

    Iter through each house and find the left and right heater. Choose the MAX of left, right heater distance.

        public int findRadius(int[] houses, int[] heaters) {
            if(houses == null || houses.length == 0) return 0;
            Arrays.sort(houses);
            Arrays.sort(heaters);
            
            int ans = 0;
            int index = 0;
            for(int house: houses) {
                while(index< heaters.length && house > heaters[index]) index++;
                
                int leftDis = index-1 < 0? Integer.MAX_VALUE:  house - heaters[index-1];
                int rightDis = index >= heaters.length?  Integer.MAX_VALUE: heaters[index] - house;
                
                int dis = Math.min(leftDis, rightDis);
                ans = Math.max(ans, dis);
            }
            
            return ans;
        }
    

Log in to reply
 

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