Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/475.html
475. Heaters
Level
Easy
Description
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.
Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.
So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.
Note:
- Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
- Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
- As long as a house is in the heaters’ warm radius range, it can be warmed.
- All the heaters follow your radius standard and the warm radius will the same.
Example 1:
Input: [1,2,3],[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: [1,2,3,4],[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.
Solution
First sort the two arrays houses
and heaters
. Then for each house
in houses
, check whether it is at the same position as any heater
in heaters
. If so, the house
is at one heater
’s position so it can be warmed no matter what the warm radius is. Otherwise, consider the heater
s adjacent to the house
and the radius must be at least the distance from the house
to its nearest heater
.
Finding the house
’s position in heaters
is done by binary search.
-
class Solution { public int findRadius(int[] houses, int[] heaters) { Arrays.sort(houses); Arrays.sort(heaters); int heatersLength = heaters.length; int minRadius = 0; for (int house : houses) { int index = binarySearch(heaters, house); if (index >= 0) continue; index = -index - 1; if (index == 0) { int curRadius = heaters[index] - house; minRadius = Math.max(minRadius, curRadius); } else if (index >= heatersLength) { int curRadius = house - heaters[heatersLength - 1]; minRadius = Math.max(minRadius, curRadius); } else { int heater1 = heaters[index - 1], heater2 = heaters[index]; int curRadius = Math.min(house - heater1, heater2 - house); minRadius = Math.max(minRadius, curRadius); } } return minRadius; } public int binarySearch(int[] array, int key) { int low = 0, high = array.length - 1; while (low <= high) { int mid = (high - low) / 2 + low; int num = array[mid]; if (num == key) return mid; else if (num > key) high = mid - 1; else low = mid + 1; } return -low - 1; } }
-
class Solution: def findRadius(self, houses: List[int], heaters: List[int]) -> int: houses.sort() heaters.sort() def check(r): m, n = len(houses), len(heaters) i = j = 0 while i < m: if j >= n: return False mi = heaters[j] - r mx = heaters[j] + r if houses[i] < mi: return False if houses[i] > mx: j += 1 else: i += 1 return True left, right = 0, int(1e9) while left < right: mid = (left + right) >> 1 if check(mid): right = mid else: left = mid + 1 return left ############ class Solution(object): def findRadius(self, houses, heaters): """ :type houses: List[int] :type heaters: List[int] :rtype: int """ minDist = 0 heaters.sort() for house in sorted(houses): idx = bisect.bisect_left(heaters, house) dist = float("inf") if idx > 0: dist = min(dist, abs(house - heaters[idx - 1])) if idx < len(heaters) - 1: dist = min(dist, abs(house - heaters[idx + 1])) if idx < len(heaters): dist = min(dist, abs(house - heaters[idx])) minDist = max(minDist, dist) return minDist
-
class Solution { public: int findRadius(vector<int>& houses, vector<int>& heaters) { sort(houses.begin(), houses.end()); sort(heaters.begin(), heaters.end()); int left = 0, right = 1e9; while (left < right) { int mid = left + right >> 1; if (check(houses, heaters, mid)) right = mid; else left = mid + 1; } return left; } bool check(vector<int>& houses, vector<int>& heaters, int r) { int m = houses.size(), n = heaters.size(); int i = 0, j = 0; while (i < m) { if (j >= n) return false; int mi = heaters[j] - r; int mx = heaters[j] + r; if (houses[i] < mi) return false; if (houses[i] > mx) ++j; else ++i; } return true; } };
-
func findRadius(houses []int, heaters []int) int { sort.Ints(houses) sort.Ints(heaters) m, n := len(houses), len(heaters) check := func(r int) bool { var i, j int for i < m { if j >= n { return false } mi, mx := heaters[j]-r, heaters[j]+r if houses[i] < mi { return false } if houses[i] > mx { j++ } else { i++ } } return true } left, right := 0, int(1e9) for left < right { mid := (left + right) >> 1 if check(mid) { right = mid } else { left = mid + 1 } } return left }
-
function findRadius(houses: number[], heaters: number[]): number { houses.sort((a, b) => a - b); heaters.sort((a, b) => a - b); const m = houses.length, n = heaters.length; let ans = 0; for (let i = 0, j = 0; i < m; i++) { let cur = Math.abs(houses[i] - heaters[j]); while ( j + 1 < n && Math.abs(houses[i] - heaters[j]) >= Math.abs(houses[i] - heaters[j + 1]) ) { cur = Math.min(Math.abs(houses[i] - heaters[++j]), cur); } ans = Math.max(cur, ans); } return ans; }