You are given an array seats where seats[i] = 1 means seat i is occupied and seats[i] = 0 means it is empty. There is at least one empty seat and at least one occupied seat.
Alex wants to sit so that the distance between him and the closest occupied seat is maximized. Return that maximum distance.
The distance to the closest person at an empty seat is the minimum gap to an occupied seat on either side. The best seat is either in the middle of the longest run of consecutive empty seats (distance (gap) / 2) or at one of the two ends (distance equal to the leading or trailing run of empty seats).
Examples
01 · Example 1
seats = [1, 0, 0, 0, 1, 0, 1] return = 2
Sitting at index 2 puts Alex 2 seats away from the closest occupied seat (indices 0 and 4).
02 · Example 2
seats = [1, 0, 0, 0] return = 3
Sitting at the last seat puts Alex 3 seats away from the only occupied seat at index 0.
03 · Example 3
seats = [0, 1] return = 1
Sitting at index 0 puts Alex distance 1 from the occupied seat at index 1.
Constraints
2 <= seats.length <= 2 * 10^4seats[i]is0or1.- At least one seat is empty.
- At least one seat is occupied.
More Roblox problems
- Candy Crush Grid Matching and GravityPHONE SCREEN · Seen Jun 2026
- Closest Binary Search Tree Value VariantPHONE SCREEN · Seen Jun 2026
- Design Search Autocomplete SystemPHONE SCREEN · Seen Jun 2026
- Find the Closest PalindromePHONE SCREEN · Seen Jun 2026
- Grid Pathfinding with Obstacles (DFS)PHONE SCREEN · Seen Jun 2026
- Maximum Number of Balls in a BoxPHONE SCREEN · Seen Jun 2026
- Meeting Rooms IIPHONE SCREEN · Seen Jun 2026
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · Seen Jun 2026
public int maxDistToClosest(int[] seats) {
// write your code here
}seats[1, 0, 0, 0, 1, 0, 1]
expected2
sign in to submit