Daily classic

Slow Sound and Searching — Li Qingzhao (Song)

Searching, cold, sad and sad. When cold and warm, the most difficult to rest. Three cups and two weak wine are no match for him. Wild goose also, is sad, but old acquaintance.

All over the ground yellow flowers pile up, gaunt and worn, now who can pick? Watching by the window, how dark alone! Wutong and drizzle, to dusk, dribs and drabs. This time, how a sorrow word get!

describe

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,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.Copy the code

Example 2:

Input: seats = [1,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.Copy the code

Example 3:

Input: seats = [0,1]
Output: 1
Copy the code

Note:

2 <= seats.length <= 2 * 10^4
seats[i] is 0 or 1.
At least one seat is empty.
At least one seat is occupied.
Copy the code

parsing

Seats [I] = 1 seats the person in the ith seat and seat[I] = 0 seats the ith seat is empty. There is at least one empty seat in this row and at least one person is sitting. There was a man named Alex who wanted to sit on the seat to maximize the distance between himself and the nearest person and return the maximum distance to the nearest person.

In fact, this problem can be solved according to our most simple idea. We find the distance between the nearest human position on the left of each position and the distance between the nearest attractive position on the right of each position and take a smaller value for comparison. However, the result of each position after comparison takes the maximum value. The key is how to find the distance between each position the distance between the left and the right. We define two lists L and R, where L[I] represents the distance between the position of index I and the nearest human to its left, and R[I] represents the distance between the position of index I and the nearest human to its right. After calculating the two lists, we compare result = Max (result, min(L[I], R[I])) by re – checking seats, and return result at the end of re – checking.

answer

class Solution(object):
    def maxDistToClosest(self, seats):
        """
        :type seats: List[int]
        :rtype: int
        """
        N = len(seats)
        L = [float('inf')] * N
        R = [float('inf')] * N
        
        for i, seat in enumerate(seats):
            if seat == 1:
                L[i] = 0
            elif i>0:
                L[i] = L[i-1] + 1
        
        for j in range(N-1, -1, -1):
            if seats[j] == 1:
                R[j] = 0
            elif j<N-1:
                R[j] = R[j+1] + 1
                
        result = 0
        for i,seat in enumerate(seats):
            result = max(result, min(L[i], R[i]))
            
        return result		
Copy the code

The results

Given in the linked list. Memory Usage: 15 MB, less than 6.29% of Python online submissions for Maximize Distance to Closest Person.Copy the code

Original link: leetcode.com/problems/ma…

Your support is my biggest motivation