![]() |
VOOZH | about |
Given a string (seats) of 1s and 0s, where 1 represents a filled seat and 0 represents an empty seat in a row. Find an empty seat with maximum distance from an occupied seat. Return the maximum distance.
Examples:
Input: Seats = "1000101"
Output: 2
Explanation: Geek can take 3rd place and have a distance of 2 in left and 2 in right.Input: Seats = "1000"
Output: 3
Explanation: Geek can take the rightmost seat to have a distance of 3.
Table of Content
This approach involves preprocessing the string to the identify the distance of the each empty seat from the nearest left occupied and right seats.
Step by Step Approach
2
We need to mainly find length of the longest subarray with 0s and place the Geek at the mid of it. So we traverse the array and keep track of empty 0s. If half of the current count is more than the current result, we update the result. One corner case that we need to handle is the case when we have 0s in the beginning and then a 1. We explicitly handle it by initializing result as -1 and then updating the result as count of 0s at the beginning.
2