zoukankan      html  css  js  c++  java
  • 849. Maximize Distance to Closest Person

    In a row of seats1 represents a person sitting in that seat, and 0 represents that the seat is empty. 

    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 closest person.

    给一个数组,要么0要么1,至少会有1个0和1个1,1表示有人坐了,0表示没有人坐。

    求找到一个位置,是的离最近的人的距离最大。

    问题就是转换成找距离最远的两个1端点,然后答案是两个1的距离除以2,当然边界条件是只有1个1,这个时候就不需要除以2

    class Solution(object):
        def maxDistToClosest(self, seats):
            """
            :type seats: List[int]
            :rtype: int
            """
            index = 0
            while index < len(seats) and seats[index] != 1:
                index += 1
            ans = index
            last = index
            for i in range(index + 1, len(seats), 1):
                if seats[i] == 1:
                    ans = max(ans, (i - last) // 2)
                    last = i
            ans = max(ans, (len(seats) - last - 1))
            return ans
  • 相关阅读:
    类图class的依赖关系
    ASP.NET MVC 5
    单例模式
    facebook .net sdk 应用
    跟我一起云计算(1)——storm
    C# 求精简用一行代码完成的多项判断 重复赋值
    语音播报实时天气
    滚动监听
    10277
    第十届蓝桥杯JavaB组省赛真题
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13301966.html
Copyright © 2011-2022 走看看