Problem Statement
The problem describes an array called seats
, each element of which can be either 0 or 1. If seats[i] = 1
, it means the i-th seat is occupied, whereas seats[i] = 0
denotes an empty seat. Alex, looking for a seat, aims to sit in such a manner that the distance to the nearest occupied seat is maximized. The task is to compute and return this maximum possible distance to the nearest person from any empty seat that Alex chooses.
Examples
Example 1
Input:
seats = [1,0,0,0,1,0,1]
Output:
2
Explanation:
If Alex sits at index 2, the closest person is at index 0 or 4 (both distance 2). This is the farthest he can be from anyone.
Example 2
Input:
seats = [1,0,0,0]
Output:
3
Explanation:
Alex should sit at the last seat (index 3), where the closest person is 3 seats away.
Example 3
Input:
seats = [0,1]
Output:
1
Explanation:
Alex can sit at index 0, which is 1 seat away from the only person sitting at index 1.
Constraints
2 <= seats.length <= 2 * 10^4
seats[i]
is0
or1
- At least one seat is empty
- At least one seat is occupied
Approach and Intuition
To solve the problem of finding the maximum distance to the closest person when choosing a seat, we follow these steps:
Pass from left to right: For each seat, track the distance to the last seen occupied seat on the left. If there’s no person to the left, set the distance to infinity.
Pass from right to left: Repeat the process but now track the distance to the closest person on the right. Again, if no person is found to the right, treat the distance as infinity.
Compute the minimum of both distances for each empty seat: This gives the shortest distance to the nearest person from both directions.
Track the maximum of these minimum distances: This represents the best possible seat Alex can choose to maximize his distance from the nearest person.
This two-pass strategy ensures an efficient O(n) solution and covers edge cases such as when Alex must sit at either end of the row.
Solutions
- Java
class Solution {
public int findMaxDistance(int[] places) {
int length = places.length;
int emptyCount = 0; // current maximum gap of zeros
int maximumDistance = 0;
for (int index = 0; index < length; ++index) {
if (places[index] == 1) {
emptyCount = 0;
} else {
emptyCount++;
maximumDistance = Math.max(maximumDistance, (emptyCount + 1) / 2);
}
}
for (int index = 0; index < length; ++index) {
if (places[index] == 1) {
maximumDistance = Math.max(maximumDistance, index);
break;
}
}
for (int index = length - 1; index >= 0; --index) {
if (places[index] == 1) {
maximumDistance = Math.max(maximumDistance, length - 1 - index);
break;
}
}
return maximumDistance;
}
}
The Java solution provided calculates the maximum distance to the closest person in a row represented as a binary array, where each element is either 0
(empty seat) or 1
(occupied seat). Follow this breakdown to understand the implementation:
- Initialize
length
to the size of the input arrayplaces
. - Use
emptyCount
to track the current sequence of consecutive empty seats (0
s), andmaximumDistance
to store the largest minimum distance found so far. - Iterate through the array:
- Reset
emptyCount
to zero when an occupied seat (1
) is found. - If an empty seat is encountered, increment
emptyCount
and updatemaximumDistance
using the formula(emptyCount + 1) / 2
to account for centering between occupied seats when possible.
- Reset
- After processing gaps within the seating, make two additional passes:
- From the beginning of the array to find the distance from the first seat to the first person seated.
- From the end of the array to measure the distance from the last seat to the last person seated.
- Compare and update
maximumDistance
based on these edge seat distances. - The method returns the computed
maximumDistance
, interpreting the scenarios where persons sit at the ends or where there may be large gaps in seating.
This approach effectively considers different seating placements ensuring maximized distance for the closest person, considering both internal gaps and edge cases.
No comments yet.