leetcode849

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode849相关的知识,希望对你有一定的参考价值。

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;
        }
    }

 

以上是关于leetcode849的主要内容,如果未能解决你的问题,请参考以下文章

LEETCODE52数组分类,简单级别,题目:717,661,746,628,643,849

[LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

LeetCode刷题笔记-数据结构-day8

LeetCode刷题笔记-数据结构-day8

849. Maximize Distance to Closest Person ——weekly contest 87

Codeforces Round #849 (Div. 4)D. Distinct Split&&E. Negatives and Positives