Heaters

Medium

Topics
ArrayTwo PointersBinary SearchSorting

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

houses123
heaters2
coverage13[1,3]worst house
the house that sets the radius
Every heater gets the same radius, so the answer is decided by whichever house sits furthest from its nearest heater — the shaded one, 1 away. The lanes show what each heater covers at that radius: once the worst house is reached, every other house already is.
Positions are drawn in sorted order; the input arrays are not necessarily sorted.
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

houses1234
heaters14
coverage05[0,2][3,5]worst house
the house that sets the radius
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

houses15
heaters2
coverage-15[-1,5]worst house
the house that sets the radius
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

  1. Sort both arrays.
  2. Sweep the houses in order, keeping an index into the heaters.
  3. 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.
  4. 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 1

Complexity

  • 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 worst
SensAI
Beta
Tap the mic to stop voice mode