C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)

版权声明:Iori 的技术分享,所有内容均为本人原创,引用请注明出处,谢谢合作! https://blog.csdn.net/qq_31116753/article/details/82505811

问题

在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的。

至少有一个空座位,且至少有一人坐在座位上。

亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。

返回他到离他最近的人的最大距离。

输入:[1,0,0,0,1,0,1]

输出:2

解释:如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。因此,他到离他最近的人的最大距离是 2 。 

输入:[1,0,0,0]

输出:3

解释: 如果亚历克斯坐在最后一个座位上,他离最近的人有 3 个座位远。这是可能的最大距离,所以答案是 3 。

提示:

1 <= seats.length <= 20000
seats 中只含有 0 和 1,至少有一个 0,且至少有一个 1。


In a row of seats, 1 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.

Input: [1,0,0,0,1,0,1]

Output: 2

Explanation: If Alex sits in the second open seat (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.

Input: [1,0,0,0]

Output: 3

Explanation: If Alex sits in the last seat, the closest person is 3 seats away.This is the maximum distance possible, so the answer is 3.

Note:

1 <= seats.length <= 20000
seats contains only 0s or 1s, at least one 0, and at least one 1.


示例

public class Program {

    public static void Main(string[] args) {
        int[] nums = null;

        nums = new int[] { 1, 1, 0, 0, 0, 1, 0 };

        var res = MaxDistToClosest(nums);
        Console.WriteLine(res);

        nums = new int[] { 1, 0, 0, 0 };
        res = MaxDistToClosest2(nums);
        Console.WriteLine(res);

        Console.ReadKey();
    }

    private static int MaxDistToClosest(int[] seats) {
        //直接解法
        int left, right;
        int min = int.MaxValue;
        int max = int.MinValue;
        for(int i = 0; i < seats.Length; i++) {
            if(seats[i] == 0) {
                //双指针找0,即空座位
                left = i; right = i;
                while(--left >= 0 && seats[left] == 0) { }
                while(++right <= seats.Length - 1 && seats[right] == 0) { }
                //处理好边界
                if(left == -1) left = int.MaxValue;
                if(right == seats.Length) right = int.MaxValue;
                //分析到最近的人的最大距离
                if(i - left < right - i && i - left >= 0) {
                    min = i - left;
                } else {
                    min = right - i;
                }
                //最大距离
                max = Math.Max(max, min);
            }
        }
        //返回最大距离
        return max;
    }

    private static int MaxDistToClosest2(int[] seats) {
        //记录所有非空座位
        var indexOfOne = new List<int>();
        for(var i = 0; i < seats.Length; i++) {
            if(seats[i] == 0) continue;
            indexOfOne.Add(i);
        }
        //找到2个非空座位的中间座位
        var max = indexOfOne[0];
        for(int i = 0, j = 1; i < indexOfOne.Count && j < indexOfOne.Count; i++, j++) {
            var halfLength = (indexOfOne[j] - indexOfOne[i]) / 2;
            if(halfLength > max) max = halfLength;
        }
        //处理边界
        var length = seats.Length - 1 - indexOfOne[indexOfOne.Count - 1];
        if(length > max) max = length;
        //返回最大距离
        return max;
    }

}

以上给出2种算法实现,以下是这个案例的输出结果:

2
3

分析:

显而易见,以上2种算法的时间复杂度均为: O(n) 。

猜你喜欢

转载自blog.csdn.net/qq_31116753/article/details/82505811