Maximize Distance to Closest Person (LeetCode 849): Solution and Explanation
Every empty stretch is either between two people or against a wall. Between people the best seat is the middle, giving (i - prev) // 2; against a wall it is the wall seat, giving the whole stretch. One scan tracking the previous person finds the maximum.
Intuition
Picture the row as people separated by runs of empty chairs. Alex only cares about the closest person, so each run can be judged on its own.
In a run between people at prev and i, moving toward one person moves away from the other. The balance point is the middle, at distance (i - prev) // 2 from the nearer one.
A run before the first person or after the last one has nobody on the wall side. Sitting right at the wall puts Alex as far as possible from the single neighbour, so that run is worth its full length.
Approach: One pass with the previous person
- Set prev = -1 and best = 0.
- Scan the seats. At each person i: if prev is -1, the leading run gives distance i; otherwise the middle run gives (i - prev) // 2. Update best, then set prev = i.
- After the scan, the trailing run gives n - 1 - prev.
- Return the maximum of best and the trailing distance.
Step-by-step example
seats = [1,0,0,0,1,0,1]
- i = 0 is the first person. The leading run is empty, so it gives 0. prev = 0.
- i = 4 is a person. The gap between 0 and 4 has its middle at seat 2, distance (4 - 0) // 2 = 2. best = 2, prev = 4.
- i = 6 is a person. The gap between 4 and 6 gives (6 - 4) // 2 = 1. best stays 2, prev = 6.
- The last seat is taken, so the trailing run gives 0.
- The answer is 2, with Alex in seat 2.
Code: Python, Java, C++
1class Solution:
2 def maxDistToClosest(self, seats: List[int]) -> int:
3 prev = -1
4 best = 0
5 for i, s in enumerate(seats):
6 if s == 1:
7 if prev == -1:
8 best = i
9 else:
10 best = max(best, (i - prev) // 2)
11 prev = i
12 return max(best, len(seats) - 1 - prev)1class Solution {
2 public int maxDistToClosest(int[] seats) {
3 int prev = -1, best = 0;
4 for (int i = 0; i < seats.length; i++) {
5 if (seats[i] == 1) {
6 if (prev == -1) {
7 best = i;
8 } else {
9 best = Math.max(best, (i - prev) / 2);
10 }
11 prev = i;
12 }
13 }
14 return Math.max(best, seats.length - 1 - prev);
15 }
16}1class Solution {
2public:
3 int maxDistToClosest(vector<int>& seats) {
4 int n = seats.size(), prev = -1, best = 0;
5 for (int i = 0; i < n; i++) {
6 if (seats[i] == 1) {
7 if (prev == -1) {
8 best = i;
9 } else {
10 best = max(best, (i - prev) / 2);
11 }
12 prev = i;
13 }
14 }
15 return max(best, n - 1 - prev);
16 }
17};Time and space complexity
O(n)
Each seat is visited once.
O(1)
Only prev and best are stored.
Other approaches
Left and right distance arrays (time O(n), space O(n))
Fill one array with the distance to the nearest person on the left and another with the distance on the right, then take the maximum over empty seats of the smaller of the two. Straightforward but uses two extra arrays.
Try every empty seat (time O(n^2), space O(1))
For each empty seat, search outward for the nearest person. Quadratic in the worst case, such as a long empty row.
Common mistakes
- Halving the end gaps. A run against a wall is worth its full length, not half of it.
- Halving the number of empty seats. With people at 0 and 4 there are 3 empty seats, and 3 // 2 = 1 is wrong: the middle seat 2 is (4 - 0) // 2 = 2 away from both.
- Forgetting the trailing run after the loop. In [1,0,0,0] the answer comes only from the end of the row.
Frequently asked questions
Why is the middle seat optimal between two people?
The distance to the closer person is min(j - prev, i - j). That minimum peaks where both terms are as equal as possible, which is the middle, and its value is (i - prev) // 2.
Do I need to return the seat index?
No, only the distance. Tracking the seat as well is easy if you want it: it is 0 for the leading run, prev + d for a middle run, and n - 1 for the trailing run.
Can the answer be 0?
No. At least one seat is empty, and any empty seat is at least 1 away from every person.