Heaters
Medium
Topics
Houses and heaters stand on a horizontal line. a holds the house positions and b holds the heater positions; neither array is necessarily sorted.
Every heater has the same radius, and a heater at position p warms every point within distance r of it. Return the minimum radius that leaves no house unwarmed.
Example 1
Input: a = [1,2,3], b = [2] Output: 1 Explanation: The single heater at 2 must reach the houses at 1 and 3, so radius 1.
Example 2
Input: a = [1,2,3,4], b = [1,4] Output: 1 Explanation: Each heater covers the house beside it; the worst house is 1 away from its nearest heater.
Example 3
Input: a = [1,5], b = [2] Output: 3 Explanation: The far house at 5 is 3 from the only heater, and that distance sets the radius.
Constraints
- 1 <= a.length, b.length <= 3 * 10^4
- 1 <= a[i], b[i] <= 10^9
Editorial — intuition, approach, complexity and pitfalls
Intuition
The radius is shared, so it is decided entirely by the worst-served house — the one furthest from any heater. Every other house is covered for free once that one is.
And a house only ever cares about its nearest heater. Heaters further away can never help it, because a radius large enough to reach from one of them is larger than the radius from the nearest one.
So the answer is: for each house, the distance to its nearest heater; then the maximum of those. Finding the nearest heater is where the sorting earns its keep — with both lists in order, a single sweep can keep a finger on the nearest heater as the houses advance, since that finger only ever moves forward.
Approach
- Sort both arrays.
- Sweep the houses in order, keeping an index into the heaters.
- Advance the heater index while the next heater is at least as close to the current house as the current one. That leaves the index on the nearest heater, and because the houses are sorted it never has to move back.
- Record the distance from the house to that heater, and return the largest distance seen.
Walkthrough
a = [1, 2, 3, 4] (houses, sorted)
b = [1, 4] (heaters, sorted)
house heater index nearest distance
1 0 (at 1) 1 0
2 0 (at 1) 1 1 next heater 4 is further
3 1 (at 4) 4 1 next heater 4 is closer
4 1 (at 4) 4 0
largest distance 1 -> radius 1Complexity
- Time — O(n log n + m log m): dominated by the two sorts; the sweep itself is linear because the heater index only moves forward.
- Space — O(n + m): the sorted copies. Sorting in place makes it O(1) beyond the inputs.
Common Pitfalls
- Taking the minimum of the per-house distances instead of the maximum. The radius has to satisfy the worst house, not the best one.
- Assuming the inputs arrive sorted. They do not, and the sweep is wrong without the sort.
- Letting the heater index move backwards, or resetting it per house. Neither is needed, and resetting turns the sweep quadratic.
- Handling only heaters to the left of a house. The nearest one can be on either side — in example 3 the only heater is to the left of one house and to the right of the other.
Full Solution — all languages
class Solution:
def findRadius(self, a, b):
houses = sorted(a)
heaters = sorted(b)
worst = 0
at = 0
for house in houses:
while at + 1 < len(heaters) and abs(heaters[at + 1] - house) <= abs(heaters[at] - house):
at += 1
worst = max(worst, abs(heaters[at] - house))
return worstimport java.util.*;
class Solution {
public int findRadius(int[] a, int[] b) {
int[] houses = a.clone(), heaters = b.clone();
Arrays.sort(houses);
Arrays.sort(heaters);
int worst = 0, at = 0;
for (int house : houses) {
while (at + 1 < heaters.length && Math.abs(heaters[at + 1] - house) <= Math.abs(heaters[at] - house)) at++;
worst = Math.max(worst, Math.abs(heaters[at] - house));
}
return worst;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findRadius(vector<int>& a, vector<int>& b) {
vector<int> houses = a, heaters = b;
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int worst = 0;
size_t at = 0;
for (int house : houses) {
while (at + 1 < heaters.size() && abs(heaters[at + 1] - house) <= abs(heaters[at] - house)) at++;
worst = max(worst, abs(heaters[at] - house));
}
return worst;
}
};/**
* @param {number[]} a
* @param {number[]} b
* @return {number}
*/ var findRadius = function(a, b) {
const houses = a.slice().sort((x, y) => x - y);
const heaters = b.slice().sort((x, y) => x - y);
let worst = 0, at = 0;
for (const house of houses) {
while (at + 1 < heaters.length && Math.abs(heaters[at + 1] - house) <= Math.abs(heaters[at] - house)) at++;
worst = Math.max(worst, Math.abs(heaters[at] - house));
}
return worst;
};