Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.

Every house can be warmed, as long as the house is within the heater's warm radius range. Given the positions of houses and heaters on a horizontal line, return the minimum radius standard of heaters so that those heaters could cover all houses.

Notice that all the heaters follow your radius standard, and the warm radius will be the same.

Input & Output

Example 1 — Basic Case
$ Input: houses = [1,2,3], heaters = [2]
Output: 1
💡 Note: The only heater is at position 2. House at 1 needs radius 1, house at 2 needs radius 0, house at 3 needs radius 1. Maximum radius needed is 1.
Example 2 — Multiple Heaters
$ Input: houses = [1,2,3,4], heaters = [1,4]
Output: 1
💡 Note: Houses at positions 1 and 4 are covered by heaters. House at 2 is closest to heater at 1 (distance 1), house at 3 is closest to heater at 4 (distance 1). Maximum radius needed is 1.
Example 3 — Single House
$ Input: houses = [1], heaters = [1,2,3,4]
Output: 0
💡 Note: The house at position 1 has a heater at the exact same position, so radius 0 is sufficient.

Constraints

  • 1 ≤ houses.length, heaters.length ≤ 3 × 104
  • 1 ≤ houses[i], heaters[i] ≤ 109

Visualization

Tap to expand
Heater Coverage Problem: houses=[1,2,3], heaters=[2]04HHH123🔥HeaterDistance from house 1 to heater: |1-2| = 1Distance from house 3 to heater: |3-2| = 1Minimum radius needed: 1
Understanding the Visualization
1
Input
Houses and heaters positioned on a line
2
Process
Find closest heater for each house
3
Output
Minimum radius to cover all houses
Key Takeaway
🎯 Key Insight: Find each house's closest heater distance, then take the maximum - this ensures all houses are covered with minimum radius
Asked in
Google 15 Amazon 12
38.0K Views
Medium Frequency
~25 min Avg. Time
1.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen