JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

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:

image tooltip here

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

Example 2:

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

Example 3:

Input: seats = [0,1]
Output: 1

Code

class Solution {
    public int maxDistToClosest(int[] seats) {
        int count = 0;
        int ans = 0;

        for (int i = 0; i < seats.length; i++) {
            if (seats[i] == 1) {
                count = 0;
            } else {
                count++;
                ans = Math.max(ans, (count + 1) / 2);
            }
        }

        // 靠墙坐
        for (int i = 0; i < seats.length; i++)
            if (seats[i] == 1) {
                ans = Math.max(ans, i);
                break;
            }

        // 靠墙坐
        for (int i = seats.length - 1; i >= 0; i--)
            if (seats[i] == 1) {
                ans = Math.max(ans, seats.length - 1 - i);
                break;
            }

        return ans;
    }
}
/**
 * 需要先找出最大的连续空位长度
 * 若连续空位靠着墙了
 * 那么就直接挨着墙坐
 * 若两边都有人
 * 那么就坐到空位的中间位置 (除2)
 */
class Solution {
    public int maxDistToClosest(int[] seats) {
        int n = seats.length;
        int prev = -1;
        int ans = 0;

        for (int i = 0; i <= n; ++i) {
            // 靠墙位置
            if (i == n) {
                ans = Math.max(ans, n - prev - 1);
            } else if (seats[i] != 0) {
                if (prev == -1) {
                    ans = i;
                } else {
                    ans = Math.max(ans, (i - prev) / 2);
                }
                prev = i;
            }
        }
        return ans;
    }
}
/**
 * 把0都放在一起考虑
 * 需要考虑特殊情况: 0是空位;最后一个位置是空位
 */
class Solution {
    public int maxDistToClosest(int[] seats) {
        int index0 = -1;
        int max = 0;
        for (int i = 0; i <= seats.length; i++) {
            if (i == seats.length) {
                if (index0 != -1) {
                    max = Math.max(max, i - index0);
                }
                break;
            }

            if (seats[i] != 0) {
                if (index0 != -1) {
                    int dist = (i - index0 + 1) / 2;
                    // 0是空位,可以靠墙坐
                    if (index0 == 0) {
                        dist = i;
                    }

                    max = Math.max(max, dist);
                    index0 = -1;
                }
            } else {
                if (index0 == -1) {
                    index0 = i;
                }
            }
        }

        return max;
    }
}