475. Heaters
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Sort Heaters + Binary Search for Each House
This problem asks for the minimum radius r required for a set of heaters to warm all houses. This single radius r must be sufficient for every house. This means that for each house, its distance to the nearest heater must be less than or equal to r. To find the minimum such r, we can find the maximum of these minimum distances. In other words, r = max(min_distance(house_i, all_heaters)) for all house_i.
Solution
Heaters in Python
import bisect
class Solution:
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
# Sort the heaters to enable binary search
heaters.sort()
n = len(heaters)
max_radius = 0
for house in houses:
# Find the insertion point for the house in the heaters array.
# bisect_left finds the index of the first element >= house.
i = bisect.bisect_left(heaters, house)
# Distance to the heater on the right
dist_right = float('inf')
if i < n:
dist_right = heaters[i] - house
# Distance to the heater on the left
dist_left = float('inf')
if i > 0:
dist_left = house - heaters[i - 1]
# Minimum distance for this house is the minimum of the two distances
current_min_dist = min(dist_left, dist_right)
# The required radius must be large enough for the 'hardest' house
max_radius = max(max_radius, current_min_dist)
return max_radius
Heaters in Java
class Solution {
public int findRadius(int[] houses, int[] heaters) {
// Sort the heaters to enable binary search
Arrays.sort(heaters);
int maxRadius = 0;
for (int house : houses) {
// Use binary search to find the insertion point of the current house
// in the heaters array.
int index = Arrays.binarySearch(heaters, house);
int insertionPoint;
if (index < 0) {
// If not found, binarySearch returns (-(insertion point) - 1).
// So, the insertion point is -(index + 1).
insertionPoint = -(index + 1);
} else {
// If found, the house is at a heater's location, distance is 0.
// We can continue to the next house.
continue;
}
// Distance to the heater on the right (if it exists)
long distRight = Long.MAX_VALUE;
if (insertionPoint < heaters.length) {
distRight = (long) heaters[insertionPoint] - house;
}
// Distance to the heater on the left (if it exists)
long distLeft = Long.MAX_VALUE;
if (insertionPoint > 0) {
distLeft = (long) house - heaters[insertionPoint - 1];
}
// Minimum distance for the current house
int currentMinDist = (int) Math.min(distLeft, distRight);
// Update the overall required radius
maxRadius = Math.max(maxRadius, currentMinDist);
}
return maxRadius;
}
}
Heaters in C++
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
// Sort the heaters to enable binary search
sort(heaters.begin(), heaters.end());
int max_radius = 0;
for (int house : houses) {
// Find the first heater that is at or after the current house's position
// lower_bound returns an iterator to the first element not less than the given value.
auto it = lower_bound(heaters.begin(), heaters.end(), house);
int idx = it - heaters.begin();
int dist_right = INT_MAX;
// If a heater is found at or after the house position
if (idx < heaters.size()) {
dist_right = heaters[idx] - house;
}
int dist_left = INT_MAX;
// If there is a heater to the left of the insertion point
if (idx > 0) {
dist_left = house - heaters[idx - 1];
}
// The minimum distance for the current house is the minimum of the two distances
int current_min_dist = min(dist_left, dist_right);
// The required radius must be at least the maximum of all minimum distances
max_radius = max(max_radius, current_min_dist);
}
return max_radius;
}
};
Heaters in TypeScript
function findRadius(houses: number[], heaters: number[]): number {
// Sort the heaters array to enable binary search.
heaters.sort((a, b) => a - b);
let maxRadius = 0;
for (const house of houses) {
// Find the insertion point of the house in the sorted heaters array.
// This is the index of the first heater with position >= house.
let i = 0;
let j = heaters.length - 1;
let insertionPoint = heaters.length;
while (i <= j) {
const mid = Math.floor((i + j) / 2);
if (heaters[mid] >= house) {
insertionPoint = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
// Distance to the heater on the right (if it exists).
let distRight = Infinity;
if (insertionPoint < heaters.length) {
distRight = heaters[insertionPoint] - house;
}
// Distance to the heater on the left (if it exists).
let distLeft = Infinity;
if (insertionPoint > 0) {
distLeft = house - heaters[insertionPoint - 1];
}
// The minimum distance for the current house.
const currentMinDist = Math.min(distLeft, distRight);
// The final radius must be at least the maximum of these minimum distances.
maxRadius = Math.max(maxRadius, currentMinDist);
}
return maxRadius;
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.