512. Heaters — Find the Minimum Radius

MediumBinary SearchBinary SearchGreedy

Given the positions of houses and heaters along a line, all heaters share the same radius. Return the minimum radius so that every house is within the radius of at least one heater. The input is JSON {houses, heaters}. Return the minimum radius.

Input: JSON {houses, heaters}.

Output: Integer — the minimum heating radius.

Examples

Example 1
Input: {"houses":[1,2,3,4],"heaters":[1,4]}
Output: 1
Explanation: Radius 1 covers all houses.
Example 2
Input: {"houses":[1,2,3],"heaters":[2]}
Output: 1
Explanation: One central heater.
Example 3
Input: {"houses":[1,5],"heaters":[2]}
Output: 3
Explanation: Farthest house is distance 3.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →