# Heaters – Leetcode Solution

In this post, we are going to solve the 475. Heaters problem of Leetcode. This problem 475. Heaters is a Leetcode medium level problem. Let’s see the code, 475. Heaters – Leetcode Solution.

## Problem

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 heater was placed in the position 1 and 4. We need to use 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`

Now, let’s see the code of 475. Heaters – Leetcode Solution.

# Heaters – Leetcode Solution

### 475. Heaters – Solution in Java

```class Solution {

public boolean searchSpace(int rad,int[] house,int[] heaters){
Arrays.sort(house);
Arrays.sort(heaters);
int i=0;
for(int heater : heaters){
for(;i<house.length;i++){
if(house[i]<l || house[i]>r)break;
}
}
return i==house.length;
}
public int findRadius(int[] houses, int[] heaters) {

int s = 0;
int e = Integer.MAX_VALUE;
int ans=0;
while(s<=e){
}
}

}
return ans;
}
}```

### 475. Heaters – Solution in C++

```class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
if (heaters.size() == 0) {
return 0;
}
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int index = 0;
for (int i = 0; i < houses.size(); i++) {
while (index + 1 < heaters.size() && (abs(heaters[index+1] - houses[i]) <= abs(heaters[index] - houses[i]))) {
index++;
}
}
}
};```

### 475. Heaters – Solution in Python

```def findRadius(self, houses, heaters):
heaters = sorted(heaters) + [float('inf')]
i = r = 0
for x in sorted(houses):
while x >= sum(heaters[i:i+2]) / 2.:
i += 1
r = max(r, abs(heaters[i] - x))
return r```

Note: This problem 475. Heaters is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.