You are given an array representing a row of seats
where seats[i] = 1
represents a person sitting in the i<sup>th</sup>
seat, and seats[i] = 0
represents that the i<sup>th</sup>
seat is empty (0-indexed).
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.
Return that maximum distance to the closest person.
Example 1:
Input: seats = [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (i.e. 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: seats = [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
Example 3:
Input: seats = [0,1]
Output: 1
Constraints:
2 <= seats.length <= 2 * 10<sup>4</sup>
seats[i]
is 0
or 1
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to find the best seat in a row such that it maximizes the distance to the nearest occupied seat. The brute force way is all about trying every single empty seat and figuring out how good it is.
Here's how the algorithm would work step-by-step:
def max_distance_to_closest(seats):
max_distance = 0
for i in range(len(seats)):
if seats[i] == 0:
# Only consider empty seats
left_distance = float('inf')
right_distance = float('inf')
for j in range(i - 1, -1, -1):
if seats[j] == 1:
left_distance = i - j
break
for j in range(i + 1, len(seats)):
if seats[j] == 1:
right_distance = j - i
break
closest_distance = min(left_distance, right_distance)
# Keeps track of maximum distance found
max_distance = max(max_distance, closest_distance)
return max_distance
The best way to solve this seating problem is to find the biggest gaps of empty seats. Then, place someone in the middle of the largest gap to maximize the distance to the nearest person. We'll handle the special cases of empty seats at the very beginning and end separately.
Here's how the algorithm would work step-by-step:
def max_dist_to_closest(seats):
max_distance = 0
empty_seats = []
current_empty_length = 0
for seat_available in seats:
if seat_available == 0:
current_empty_length += 1
else:
if current_empty_length > 0:
empty_seats.append(current_empty_length)
current_empty_length = 0
if current_empty_length > 0:
empty_seats.append(current_empty_length)
longest_stretch = 0
longest_stretch_index = -1
for index, length in enumerate(empty_seats):
if length > longest_stretch:
longest_stretch = length
longest_stretch_index = index
# Place a person in the middle of the longest stretch.
if longest_stretch_index != -1:
start_index = 0
for i in range(longest_stretch_index):
start_index += empty_seats[i] + 1
seats[start_index + longest_stretch // 2] = 1
# Handle empty seats at the beginning.
if seats[0] == 0:
seats[0] = 1
# Handle empty seats at the end.
if seats[-1] == 0:
seats[-1] = 1
last_person = -1
for i in range(len(seats)):
if seats[i] == 1:
if last_person == -1:
max_distance = i
else:
max_distance = max(max_distance, (i - last_person) // 2)
last_person = i
# Distance to the end if the last person is not at the end.
max_distance = max(max_distance, len(seats) - 1 - last_person)
return max_distance
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the input array is null or empty since no seats exist. |
Array with only one seat | If the array has only one seat, return 1 if it's empty (0), and 0 if it's occupied (1). |
All seats are occupied (all 1s) | Return 0 since no empty seat is available to maximize distance. |
All seats are empty (all 0s) | Return the length of the array since the person can sit at either end and maximize the distance. |
An empty seat is at the beginning of the row | The distance to the closest person is simply the index of the first occupied seat. |
An empty seat is at the end of the row | The distance to the closest person is the length of the array minus one minus the index of the last occupied seat. |
Consecutive empty seats in the middle of the row | The distance to the closest person is half the number of consecutive empty seats (rounded up) or the distances to left/right, depending on your implementation. |
Integer overflow when calculating the distance if array size is close to maximum integer value | Use long data type to avoid potential integer overflow when calculating distances. |