Problem Description
Given positions of houses and heaters on a horizontal line, determine the minimum radius standard for all heaters such that every house is within the warm radius of at least one heater.
Key Insights
- Sort both the houses and the heaters arrays to efficiently search for the nearest heater for any given house.
- For each house, use binary search to locate the heater that is closest.
- The minimum required radius is the maximum distance a house is from its nearest heater.
- The problem leverages binary search for each house, leading to an overall efficient solution.
Space and Time Complexity
Time Complexity: O(n log m) where n is the number of houses and m is the number of heaters. Space Complexity: O(1) (ignoring the space required for input sorting, which can be done in-place)
Solution
The solution first sorts the houses and heaters arrays. For each house, binary search is applied to the heaters array to find the position where the heater value is just greater than or equal to the house's position. The distance to the closest heater is then the minimum of the distance from the house to the heater just before or just after this position. Finally, the answer is the maximum of these minimum distances, ensuring that every house is within the coverage of some heater.