# 849. Maximize Distance to Closest Person

In a row of `seats`, `1` represents a person sitting in that seat, and `0` represents that the seat is empty.&#x20;

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.&#x20;

Return that maximum distance to closest person.

**Example 1:**

```
Input: [1,0,0,0,1,0,1]
Output: 2
Explanation: 
If Alex sits in the second open seat (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.
```

**Example 2:**

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

**Note:**

1. `1 <= seats.length <= 20000`
2. `seats` contains only 0s or 1s, at least one `0`, and at least one `1`.

#### 解题要点：

大佬亲自操作，学习一下。设定两个新数组，一个从前开始遍历，一个从后，计算出距离1的距离比如：原数组\[0,0,1,0,0,0]，从前开始：\[0,1,0,1,2,3]，从后开始：\[2,1,0,2,1,0]。

```java
class Solution {
    public int maxDistToClosest(int[] seats) {
        int[] front = new int[seats.length];
        int[] back = new int[seats.length];
        
        for(int i = 1; i < seats.length; i++){
            if(seats[i] == 0){
                front[i] = front[i - 1] + 1;
            }
        }
        
        for(int i = seats.length - 2; i >= 0; i--){
            if(seats[i] == 0){
                back[i] = back[i + 1] + 1;
            }
        }
        int res = 0;
        for(int i = 0; i < seats.length; i ++){
            res = Math.max(res, Math.min(front[i],back[i]));
        }
        res = Math.max(front[seats.length - 1], res);
        res = Math.max(back[0], res);
        return res;
    }
}
```
