Simple C# Solution


  • 0
    A
    public int FindRadius(int[] houses, int[] heaters)
            {
                houses = houses.OrderBy(x => x).ToArray();
                heaters = heaters.OrderBy(x => x).ToArray();
    
                int ret = 0;
                for (int i = 0, j = 0; i < heaters.Length && j < houses.Length;)
                {
                    if (houses[j] <= heaters[i] && i == 0)
                    {
                        ret = Math.Max(ret, heaters[i] - houses[j]);
                        j++;
                    }
                    else if (houses[j] >= heaters[i] && i == heaters.Length - 1)
                    {
                        ret = Math.Max(ret, houses[j] - heaters[i]);
                        j++;
                    }
                    else if (i!= heaters.Length - 1 && houses[j] >= heaters[i] && houses[j] <= heaters[i + 1])
                    {
                        ret = Math.Max(ret, Math.Min(houses[j] - heaters[i], heaters[i + 1] - houses[j]));
                        j++;
                    }
                    else
                    {
                        i++;
                    }
                }
    
                return ret;
            }
    

Log in to reply
 

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