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

# Maximize Distance to Closest Person

> Tested Python solution for LeetCode 849 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 849, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array). [View on LeetCode](https://leetcode.com/problems/maximize-distance-to-closest-person/description/).

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

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

## Problem

You are given an array representing a row of `seats` where `seats[i] = 1` represents a person sitting in the `i<sup>th</sup>` seat, and `seats[i] = 0` represents that the `i<sup>th</sup>` seat is empty **(0-indexed)**.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return *that maximum distance to the closest person*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/10/distance.jpg)

```
Input: seats = [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.
```

```
Input: seats = [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
```

```
Input: seats = [0,1]
Output: 1
```

### Constraints

* 2 \<= seats.length \<= 2 \* 10^4
* seats\[i] is 0 or 1.
* At least one seat is empty.
* At least one seat is occupied.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_dist_to_closest(self, seats: list[int]) -> int:
        best = 0
        prev = -1
        n = len(seats)
        for i, occupied in enumerate(seats):
            if occupied:
                best = i if prev < 0 else max(best, (i - prev) // 2)
                prev = i
        return max(best, n - 1 - prev)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


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