my 18ms java solution; beats 94%


  • 1
    Z

    '''
    public class Solution {
    public int findRadius(int[] houses, int[] heaters) {
    int result = 0;
    int min=0;
    Arrays.sort(houses);
    Arrays.sort(heaters);
    for(int i=0,j=0;i<houses.length&&j<heaters.length;){
    {
    if(houses[i]<=heaters[j]){
    if(j==0)
    min = heaters[j]-houses[0];
    else
    min = Math.min(heaters[j]-houses[i],houses[i]-heaters[j-1]);
    i++;
    result = Math.max(min,result);
    }
    else if(j==heaters.length-1)
    return result = Math.max(result, houses[houses.length-1]-heaters[j]);
    else
    j++;
    }
    }

        return result;
    

    '''
    }
    }


  • 0
    G

    This is very good solution and beats 85% of java solutions. I am reposting with correct formatting :

    public class Solution {
        
        public int findRadius(int[] houses, int[] heaters) {
            int result = 0;
            int min=0;
            Arrays.sort(houses);
            Arrays.sort(heaters);
            for(int i=0,j=0;i<houses.length&&j<heaters.length;){
            {
                if(houses[i]<=heaters[j]){
                    if(j==0)
                        min = heaters[j]-houses[0];
                    else
                        min = Math.min(heaters[j]-houses[i],houses[i]-heaters[j-1]);
                    i++;
                    result = Math.max(min,result);
                }
                else if(j==heaters.length-1)
                    return result = Math.max(result, houses[houses.length-1]-heaters[j]);
                else
                    j++;
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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