You are given an array representing a row of seats
where seats[i] = 1
represents a person sitting in the ith
seat, and seats[i] = 0
represents that the ith
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 * 104
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 is like arranging people on a bench to maximize the distance to the nearest person. The brute force way to solve this involves trying every possible seat for the new person. We'll check the distance to others for each possibility and select the best one.
Here's how the algorithm would work step-by-step:
def max_dist_to_closest(seats):
max_distance = 0
for empty_seat_index in range(len(seats)):
if seats[empty_seat_index] == 0:
# Found an empty seat; now find closest person.
min_person_distance = float('inf')
for person_index in range(len(seats)):
if seats[person_index] == 1:
distance = abs(empty_seat_index - person_index)
min_person_distance = min(min_person_distance, distance)
# Keep track of the maximum closest distance.
max_distance = max(max_distance, min_person_distance)
return max_distance
The goal is to find the largest possible gap between a seat and the nearest person. We can solve this efficiently by scanning the row of seats to find the sizes of all the empty spaces and then choosing the largest one, while paying special attention to the ends.
Here's how the algorithm would work step-by-step:
def max_dist_to_closest(seats):
max_distance = 0
last_person_position = -1
# Handle empty seats at the beginning
for i, seat in enumerate(seats):
if seat == 1:
max_distance = i
last_person_position = i
break
elif i == len(seats) - 1:
max_distance = len(seats) #All seats are empty
return max_distance
# Find empty seats between people
for i in range(last_person_position + 1, len(seats)):
if seats[i] == 1:
distance = i - last_person_position - 1
# Distance is halved because person can sit in the middle
max_distance = max(max_distance, distance // 2 + (distance % 2))
last_person_position = i
# Handle empty seats at the end
max_distance = max(max_distance, len(seats) - 1 - last_person_position)
return max_distance
Case | How to Handle |
---|---|
Null or empty input array | Return 0 or throw an IllegalArgumentException, based on requirements, since there are no seats. |
Input array with only one element | Return 0 since the problem states there is at least one empty seat and one person. |
Input array with all seats occupied (all 1s) | Return 0 since there must be at least one empty seat. |
Input array with all seats empty (all 0s) | Return the length of the array, as the maximum distance would be from either end. |
Empty seat at the beginning of the array | Track the distance from the beginning to the first occupied seat, and update the maximum distance accordingly. |
Empty seat at the end of the array | Track the distance from the last occupied seat to the end, and update the maximum distance accordingly. |
Multiple consecutive empty seats in the middle of the array | Calculate half the distance between the surrounding occupied seats and update the maximum distance if necessary. |
Large input array size (potential performance bottleneck) | Ensure the solution uses a linear time complexity algorithm (O(n)) to efficiently iterate through the seats array. |