public class Solution
    {
        public int MaxDistToClosest(int[] seats)
        {

            int lastST = seats.Length - 1;
            var len = seats.Length;

            if (len < 3)
            {
                return len - 1;
            }
            var list = new List<int>();
            for (int i = 0; i < len; i++)
            {
                if (seats[i] == 1)
                {
                    list.Add(i);
                }
            }

            var begin = 0;
            var end = 0;
            var maxlen = 0;
            var position = -1;
            for (int i = 1; i < list.Count; i++)
            {
                var dis = list[i] - list[i - 1];
                if (maxlen * 2 < dis)
                {
                    maxlen = dis / 2;
                    begin = list[i - 1];
                    end = list[i];
                }
            }
            position = (end - begin) / 2;

            if (list[0] != 0)//第一个有人的位置不是最左边
            {
                if (maxlen < list[0])
                {
                    maxlen = list[0];
                    position = 0;
                }
            }
            if (list[list.Count - 1] != len - 1)
            {
                if (maxlen < len - list[list.Count - 1] - 1)
                {
                    maxlen = len - list[list.Count - 1] - 1;
                    position = len - 1;
                }
            }

            return maxlen;
        }
    }

 

posted on 2018-10-01 13:56  Sempron2800+  阅读(77)  评论(0编辑  收藏  举报