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 the same.
Example 1:
Input: houses = [1,2,3], heaters = [2] Output: 1 Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
Example 2:
Input: houses = [1,2,3,4], heaters = [1,4] Output: 1 Explanation: The two heaters were placed at positions 1 and 4. We need to use a radius 1 standard, then all the houses can be warmed.
Example 3:
Input: houses = [1,5], heaters = [2] Output: 3
Constraints:
1 <= houses.length, heaters.length <= 3 * 104
1 <= houses[i], heaters[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have houses along a line and you need to find the smallest radius of heaters to warm all the houses. The brute force approach is like trying every possible radius, one by one, to see if it covers all the houses. We check each house for each radius to see if it's warmed.
Here's how the algorithm would work step-by-step:
def find_smallest_heater_radius_brute_force(houses, heaters):
radius = 0
while True:
house_is_warmed_by_heaters = True
# Check if every house is warmed by at least one heater with the current radius
for house in houses:
house_is_warmed = False
for heater in heaters:
if abs(house - heater) <= radius:
house_is_warmed = True
break
if not house_is_warmed:
house_is_warmed_by_heaters = False
break
# If all houses are warmed, return the current radius
if house_is_warmed_by_heaters:
return radius
# Increment the radius and try again
radius += 1
The goal is to find the minimum radius each heater needs to cover all houses. We will leverage the sorted nature of both houses and heaters to efficiently determine the closest heater for each house, optimizing the search process.
Here's how the algorithm would work step-by-step:
def findRadius(houses, heaters):
heaters.sort()
maximum_radius = 0
for house_position in houses:
# Need to find closest heater for each house.
left_heater_index = 0
right_heater_index = len(heaters) - 1
while left_heater_index < right_heater_index:
middle_heater_index = (left_heater_index + right_heater_index) // 2
if heaters[middle_heater_index] < house_position:
left_heater_index = middle_heater_index + 1
else:
right_heater_index = middle_heater_index
# Binary search gives us the index of the heater >= house
closest_right_heater_distance = float('inf')
if left_heater_index < len(heaters):
closest_right_heater_distance = heaters[left_heater_index] - house_position
closest_left_heater_distance = float('inf')
if left_heater_index > 0:
closest_left_heater_distance = house_position - heaters[left_heater_index - 1]
# Determining the closest heater
minimum_heater_distance = min(closest_left_heater_distance, closest_right_heater_distance)
maximum_radius = max(maximum_radius, minimum_heater_distance)
return maximum_radius
Case | How to Handle |
---|---|
Empty houses array | Return 0 as no houses need heating when the houses array is empty. |
Empty heaters array | Return infinity as no houses can be heated if there are no heaters. |
Single house and single heater | Return the absolute difference between their positions. |
All houses are located at the same position | Find the minimum distance to the nearest heater for that specific location. |
All heaters are located at the same position | Find the maximum of the minimum distances between each house and that heater position. |
Houses and heaters are already sorted | The algorithm should still work efficiently, potentially optimizing early exits. |
Houses are located far away from heaters (potential integer overflow) | Use long integers for calculations to avoid potential integer overflow problems when calculating distances. |
Extremely large number of houses and heaters (scaling issues) | Ensure the algorithm's time complexity is optimized, ideally O(n log m), where n is houses and m is heaters, using binary search. |