We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你一个数组 seats 表示一排座位,其中 seats[i] = 1 代表有人坐在第 i 个座位上,seats[i] = 0 代表座位 i 上是空的(下标从 0 开始)。
至少有一个空座位,且至少有一人已经坐在座位上。
亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
返回他到离他最近的人的最大距离。
输入:seats = [1,0,0,0,1,0,1] 输出:2 解释: 如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。 如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。 因此,他到离他最近的人的最大距离是 2 。
输入:seats = [1,0,0,0] 输出:3 解释: 如果亚历克斯坐在最后一个座位上,他离最近的人有 3 个座位远。 这是可能的最大距离,所以答案是 3 。
输入:seats = [0,1] 输出:1
The text was updated successfully, but these errors were encountered:
class Solution { /** * @param Integer[] $seats * @return Integer */ function maxDistToClosest($seats) { $ret = 0; $left = 0; while ($left < count($seats) && $seats[$left] === 0) { $left++; $ret = max($ret, $left); } while ($left < count($seats)) { $right = $left + 1; while ($right < count($seats)) { if ($seats[$right] === 1) { break; } else { $right++; } } if ($right === count($seats)) { $ret = max($ret, $right - $left - 1); } else { $ret = max($ret, intval(($right - $left)/2)); } $left = $right; } return $ret; } }
Sorry, something went wrong.
No branches or pull requests
给你一个数组 seats 表示一排座位,其中 seats[i] = 1 代表有人坐在第 i 个座位上,seats[i] = 0 代表座位 i 上是空的(下标从 0 开始)。
至少有一个空座位,且至少有一人已经坐在座位上。
亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
返回他到离他最近的人的最大距离。
示例 1:
示例 2:
示例 3:
提示:
The text was updated successfully, but these errors were encountered: