475. Heaters LeetCode Solution
In this guide, you will get 475. Heaters LeetCode Solution with the best time and space complexity. The solution to Heaters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Heaters solution in C++
- Heaters solution in Java
- Heaters solution in Python
- Additional Resources

Problem Statement of Heaters
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
Complexity Analysis
- Time Complexity: \max(O(m\log m), O(n\log n)), where m = |\texttt{houses}| and n = |\texttt{heaters}|
- Space Complexity: O(1)
475. Heaters LeetCode Solution in C++
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
ranges::sort(houses);
ranges::sort(heaters);
int ans = 0;
int i = 0; // heaters' index (currently used)
for (const int house : houses) {
while (i + 1 < heaters.size() &&
house - heaters[i] > heaters[i + 1] - house)
++i; // The next heater is better.
ans = max(ans, abs(heaters[i] - house));
}
return ans;
}
};
/* code provided by PROGIEZ */
475. Heaters LeetCode Solution in Java
class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters);
int ans = 0;
int i = 0; // heaters' index (currently used)
for (final int house : houses) {
while (i + 1 < heaters.length && house - heaters[i] > heaters[i + 1] - house)
++i; // The next heater is better.
ans = Math.max(ans, Math.abs(heaters[i] - house));
}
return ans;
}
}
// code provided by PROGIEZ
475. Heaters LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.