---
# System prepended metadata

title: 0849. Maximize Distance to Closest Person
tags: [Leetcode, Medium]

---

# 0849. Maximize Distance to Closest Person
###### tags: `Leetcode` `Medium`
Link: https://leetcode.com/problems/maximize-distance-to-closest-person/
## 思路 $O(N)$ $O(1)$
分三种情况讨论
The final result = $max(distance \ at \ the \ beginning, distance \ in \ the \ middle / 2, distance \ at \ the \ end)$
## Code
```java=
class Solution {
    public int maxDistToClosest(int[] seats) {
        int maxDist = 0;
        int prevSeat = 0;
        int startEmpty = 0, endEmpty = 0;
        for(int i = 0;i < seats.length;i++){
            if(seats[i]==1){
                //if the start seat is empty
                if(prevSeat==0 && seats[0]==0) startEmpty = i;
                maxDist = Math.max(maxDist, i-prevSeat);
                prevSeat = i;
            }
        }
        //if the last seat is empty
        if(seats[seats.length-1]==0) endEmpty = seats.length-1-prevSeat;
        maxDist = (int)(maxDist/2);
        return Math.max(maxDist, Math.max(startEmpty, endEmpty));
    }
}
```