Created
August 13, 2018 09:05
-
-
Save luojiyin1987/00e2c39c58d74e52abbecb10773f3a07 to your computer and use it in GitHub Desktop.
Maximize Distance to Closest Person
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def maxDistToClosest(self, seats): | |
| """ | |
| :type seats: List[int] | |
| :rtype: int | |
| """ | |
| hasSeats = [] | |
| len_seats = len(seats) | |
| count = 0 | |
| for seat in seats: | |
| if seat == 1: | |
| hasSeats.append(count) | |
| count +=1 | |
| last = hasSeats[0] | |
| max_len = 0 | |
| for num in hasSeats[1:]: | |
| if num -last > max_len: | |
| max_len = num -last | |
| last = num | |
| max_len = max_len //2 | |
| if 0 not in hasSeats: | |
| headLength = hasSeats[0] - 0 | |
| if headLength > max_len: | |
| max_len = headLength | |
| if (len_seats-1) not in hasSeats: | |
| endLength = len_seats -1 - hasSeats[-1] | |
| if endLength > max_len: | |
| max_len = endLength | |
| return max_len |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment