Heaters - Complete Solution Guide
Heaters is LeetCode problem 475, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. Example 1: Input: houses =
Detailed Explanation
The problem asks us to find the minimum radius needed for heaters to cover all houses on a horizontal line. We are given the positions of houses and heaters. The goal is to determine the smallest radius such that every house is within the range of at least one heater. Each heater will have the same radius.
Solution Approach
The solution first sorts the heaters' positions. Then, for each house, it uses binary search to find the closest heater. Once the closest heater (or the index where the house would be inserted) is found, the algorithm calculates the distances to the nearest heaters on the left and right. The minimum of these two distances represents the minimum radius required to cover the current house. Finally, the algorithm takes the maximum of these minimum distances across all houses, which is the required minimum radius.
Step-by-Step Algorithm
- Step 1: Sort the `heaters` array in ascending order. This enables the use of binary search.
- Step 2: Initialize `radius` to 0. This variable will store the minimum radius needed to cover all houses.
- Step 3: Iterate through each `house` in the `houses` array.
- Step 4: For each `house`, use binary search (or `bisect_left` in Python) to find the index of the closest heater or the index where the house would be inserted in the sorted `heaters` array.
- Step 5: Calculate the distance to the nearest heater on the left (`dist_left`). If the index found in step 4 is 0, the house is to the left of all heaters, so `dist_left` is set to infinity. Otherwise, `dist_left` is the difference between the house and the heater to its immediate left.
- Step 6: Calculate the distance to the nearest heater on the right (`dist_right`). If the index found in step 4 is equal to the size of the `heaters` array, the house is to the right of all heaters, so `dist_right` is set to infinity. Otherwise, `dist_right` is the difference between the heater at the found index and the house.
- Step 7: Determine the minimum distance required to cover the current house by taking the minimum of `dist_left` and `dist_right`.
- Step 8: Update the `radius` by taking the maximum of the current `radius` and the minimum distance calculated in step 7.
- Step 9: After iterating through all houses, return the final `radius`.
Key Insights
- Insight 1: Sorting the heaters allows us to use binary search to efficiently find the nearest heaters to each house.
- Insight 2: For each house, we only need to consider the distance to its closest heater on the left and the distance to its closest heater on the right.
- Insight 3: The minimum radius is the maximum of the minimum distances calculated for each house.
Complexity Analysis
Time Complexity: O(n log m)
Space Complexity: O(1)
Topics
This problem involves: Array, Two Pointers, Binary Search, Sorting.
Companies
Asked at: Anduril, DE Shaw, Intuit, Nutanix, PhonePe.