Java Binary Search Solution


  • 0
    B
    public class Solution {
        public int findRadius(int[] houses, int[] heaters) {
            Arrays.sort(heaters);
            int res = 0;
            for(int h : houses){
                int low = 0, high = heaters.length - 1;
                while(low + 1 < high){
                    int mid = low + (high - low)/2;
                    if(heaters[mid] >= h){
                        high = mid;
                    }else{
                        low = mid ;
                    }
                }
                res = Math.max(res, Math.min(Math.abs(heaters[low] - h),Math.abs(heaters[high] - h)));
            }
            return res;
        }
    }

Log in to reply
 

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