> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/leetcode/binary-search/774-minimize-max-distance-to-gas-station.md).

# 774 Minimize Max Distance to Gas Station

## 774. [Minimize Max Distance to Gas Station](https://leetcode.com/problems/minimize-max-distance-to-gas-station/description/)

## 1. Question

On a horizontal number line, we have gas stations at positions`stations[0], stations[1], ..., stations[N-1]`, where`N = stations.length`.

Now, we add`K`more gas stations so that **D**, the maximum distance between adjacent gas stations, is minimized.

Return the smallest possible value of **D**.

**Example:**

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

Output: 0.500000
```

**Note:**

1. `stations.length`will be an integer in range`[10, 2000]`.
2. `stations[i]`will be an integer in range`[0, 10^8]`.
3. `K`will be an integer in range`[1, 10^6]`.
4. Answers within`10^-6`of the true value will be accepted as correct.

## 2. Implementation

**(1) Binary Search**

思路: 题目要求要在stations数组中放入最多K个加油站，使stations之间的最大距离最小。这是一道根据函数判断二分方向的二分法题，这本质上是通过二分法**找解的右边界， 我们要找到一个最小的distance，使得 K \* distance >= 原来站之间的距离和。**

由于stationss\[i]的范围在0和10^8之间，那么将start初始为0, end初始为10^8，然后按照这个距离二分找出满足条件的距离。而isValid()就是判断二分方向的函数，其核心就是根据我们二分得到的candidate mid这个距离，我们计算stations之间按照这个距离评分的话我们需要多少个加油站，如果需要的加油站count > K，说明mid这个距离太小，所以start = mid, 如果count <= K,说明mid是个可能的解，将end = mid。&#x20;

```java
class Solution {
    public double minmaxGasDist(int[] stations, int K) {
        double end = Integer.MIN_VALUE;

        for (int i = 1; i < stations.length; i++) {
            end = Math.max(end, stations[i] - stations[i - 1]);
        }

        double start = 0, mid = 0;

        while (end - start > 1e-6) {
            mid = start + (end - start) / 2;

            if (isValid(stations, K, mid)) {
                end = mid;
            }
            else {
                start = mid;
            }
        }
        return start;
    }

    public boolean isValid(int[] stations, int K, double target) {
        int count = 0;
        for (int i = 1; i < stations.length; i++) {
            count += (stations[i] - stations[i - 1]) / target;
        }
        return count <= K;
    }
}
```

## 3. Time & Space Complexity

**Binary Search:** 时间复杂度O(nlogm), n为stations的个数，M是原来stations之间的距离和。 空间复杂度O(1)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/binary-search/774-minimize-max-distance-to-gas-station.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
