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

# Minimize Max Distance to Gas Station

> Tested Python solution for LeetCode 774 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 774, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/).

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

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

## Problem

You are given an integer array `stations` that represents the positions of the gas stations on the x-axis. You are also given an integer `k`.

You should add `k` new gas stations. You can add the stations anywhere on the x-axis, and not necessarily on an integer position.

Let `penalty()` be the **maximum** distance between adjacent gas stations after adding the `k` new stations.

Return the smallest possible value of `penalty()`. Answers within `10^-6` of the actual answer will be accepted.

### Examples

```
Input: stations = [1,2,3,4,5,6,7,8,9,10], k = 9
Output: 0.50000
```

```
Input: stations = [23,24,36,39,46,56,57,65,84,98], k = 1
Output: 14.00000
```

### Constraints

* 10 \<= stations.length \<= 2000
* 0 \<= stations\[i] \<= 10^8
* stations is sorted in a strictly increasing order.
* 1 \<= k \<= 10^6

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log M) where M = max gap
    # Space: O(1)
    def minmax_gas_dist(self, stations: list[int], k: int) -> float:
        gaps = [stations[i + 1] - stations[i] for i in range(len(stations) - 1)]

        def check(x: float) -> bool:
            return sum(int(g / x) for g in gaps) <= k

        left, right = 0.0, float(max(gaps))
        while right - left > 1e-6:
            mid = (left + right) / 2
            if check(mid):
                right = mid
            else:
                left = mid
        return left
```

## Complexity

| Time | Space |
| - | - |
| O(n log M) where M = max gap | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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