> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Heaters Python Solution with Tests

> Tested Python solution for LeetCode 475 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 475, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/heaters/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 475   # by problem number
lcpy gen -s heaters   # by problem name
```

## 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 be the same.

### Examples

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

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

```
Input: houses = [1,5], heaters = [2]
Output: 3
```

### Constraints

* `1 <= houses.length, heaters.length <= 3 * 10^4`
* `1 <= houses[i], heaters[i] <= 10^9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/heaters/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/heaters/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import bisect


class Solution:
    # Time: O(n log n + m log m) for the sorts, then O(m log n) lookups
    # Space: O(1) extra beyond the in-place sorts
    def find_radius(self, houses: list[int], heaters: list[int]) -> int:
        houses.sort()
        heaters.sort()
        radius = 0
        for house in houses:
            i = bisect.bisect_left(heaters, house)
            dists: list[int] = []
            if i > 0:
                dists.append(house - heaters[i - 1])
            if i < len(heaters):
                dists.append(heaters[i] - house)
            radius = max(radius, min(dists))
        return radius
```

## Complexity

| Time | Space |
| - | - |
| O(n log n + m log m) for the sorts, then O(m log n) lookups | O(1) extra beyond the in-place sorts |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.