# Heaters LeetCode Solution

Here, We see Heaters LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## 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.

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

## Heaters LeetCode SolutionC++

``````class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end());
for(auto house : houses) {
if(house <= heaters.front()) {
continue;
}
if(house >= heaters.back()) {
continue;
}
}
}
private:
int findAjacentHeaters(int house, vector<int>& heaters) {
int l = 0;
int r = heaters.size() - 1;
while(l <= r) {
int mid = l + (r - l) / 2;
if(heaters[mid] == house) return 0;
if(heaters[mid] < house) l = mid + 1;
if(heaters[mid] > house) r = mid - 1;
}
return min(heaters[l]-house, house-heaters[r]);
}
};```Code language: PHP (php)```

## Heaters LeetCode SolutionJava

``````class Solution {
public int findRadius(int[] houses, int[] heaters) {
if(houses == null || houses.length == 0 || heaters == null || heaters.length == 0){
return 0;
}
Arrays.sort(houses);
Arrays.sort(heaters);
int n = houses.length;
int m = heaters.length;
int i = 0;
int j = 0;
int res = 0;
while(i < n && j < m){
int dist1 = Math.abs(heaters[j] - houses[i]);
int dist2 = Integer.MAX_VALUE;
if(j + 1 < m){
dist2 = Math.abs(heaters[j + 1] - houses[i]);
}
if(dist1 < dist2){
res = Math.max(res, dist1);
i++;
}else{
j++;
}
}
return res;
}
}```Code language: JavaScript (javascript)```

## Heaters SolutionJavaScript

``````var findRadius = function(houses, heaters) {
heaters.sort((a, b) => a - b);
return Math.max(...houses.map(h => findMinDistance(h, heaters)));
};
const findMinDistance = (house, heaters) => {
let left = 0;
let right = heaters.length - 1;
while (left <= right) {
const mid = left + ((right - left) >> 1);
if (heaters[mid] <= house && house <= heaters[mid + 1]) {
return Math.min(house - heaters[mid], heaters[mid + 1] - house);
} else if (heaters[mid] <= house) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (left === 0) return heaters[0] - house;
if (left === heaters.length) return house - heaters[heaters.length - 1];
};```Code language: JavaScript (javascript)```

## Heaters SolutionPython

``````class Solution(object):