#475
Medium Algorithms Heaters
Array Two Pointers Binary Search Sorting
41.5% acceptance
Jan 13, 2026
2345
1198
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.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn find_radius(houses: Vec<i32>, mut heaters: Vec<i32>) -> i32 {
heaters.sort();
let mut max_radius = 0;
for &house in &houses {
let pos = heaters.binary_search(&house).unwrap_or_else(|x| x);
let mut min_dist = i32::MAX;
if pos < heaters.len() {
min_dist = min_dist.min((heaters[pos] - house).abs());
}
if pos > 0 {
min_dist = min_dist.min((heaters[pos - 1] - house).abs());
}
max_radius = max_radius.max(min_dist);
}
max_radius
}
}