Class Solution
java.lang.Object
g0801_0900.s0849_maximize_distance_to_closest_person.Solution
849 - Maximize Distance to Closest Person.<p>Medium</p>
<p>You are given an array representing a row of <code>seats</code> where <code>seats[i] = 1</code> represents a person sitting in the <code>i<sup>th</sup></code> seat, and <code>seats[i] = 0</code> represents that the <code>i<sup>th</sup></code> seat is empty <strong>(0-indexed)</strong>.</p>
<p>There is at least one empty seat, and at least one person sitting.</p>
<p>Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.</p>
<p>Return <em>that maximum distance to the closest person</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2020/09/10/distance.jpg" alt="" /></p>
<p><strong>Input:</strong> seats = [1,0,0,0,1,0,1]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.</p>
<p>If Alex sits in any other open seat, the closest person has distance 1.</p>
<p>Thus, the maximum distance to the closest person is 2.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> seats = [1,0,0,0]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.</p>
<p>This is the maximum distance possible, so the answer is 3.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> seats = [0,1]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= seats.length <= 2 * 10<sup>4</sup></code></li>
<li><code>seats[i]</code> is <code>0</code> or <code>1</code>.</li>
<li>At least one seat is <strong>empty</strong>.</li>
<li>At least one seat is <strong>occupied</strong>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxDistToClosest
public int maxDistToClosest(int[] seats)
-