A row contains occupied and unoccupied seats. Each seat is represented by "O" when occupied or "U" when unoccupied.
Find the unoccupied seat whose distance from its nearest occupied seat is as large as possible. The distance between two seats is the absolute difference between their zero-based positions.
If multiple unoccupied seats have the same maximum distance, return the smallest position among them.
SeatDistanceFinder
SeatDistanceFinder()
int findSeatPosition(List<String> seats)
seats.get(i) represents the seat at zero-based position i."O" represents an occupied seat."U" represents an unoccupied seat.i, its distance is the minimum value of abs(i - j) over all occupied positions j.2 ≤ seats.size() ≤ 100,000seats is either "O" or "U".seats and all its elements are non-null.O(n) time.O(1) additional space. findSeatPosition( seats = List.of("U", "U", "O", "U", "U", "U", "O", "U"))
Output: 0
Positions 0 and 4 are both two seats away from their nearest occupied seat. Position 0 is returned because it is smaller.
findSeatPosition( seats = List.of("O", "U", "O", "U", "U", "U", "U"))
Output: 6
Position 6 is four seats away from the nearest occupied seat, which is farther than every other unoccupied seat.
findSeatPosition( seats = List.of("U", "O", "U", "U", "U", "O", "U"))
Output: 3
Position 3 is two seats away from its nearest occupied seat.