diff --git a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/1.jpg b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/1.jpg new file mode 100644 index 000000000..0bd9c798d Binary files /dev/null and b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/1.jpg differ diff --git a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/README.md b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/README.md index 3503ac44b..c57cd48a2 100644 --- a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/README.md +++ b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/README.md @@ -1,28 +1,43 @@ # [849.Maximize Distance to Closest Person][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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:** -**Example 1:** +![1](./1.jpg) ``` -Input: a = "11", b = "1" -Output: "100" +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:** -## 题解 - -### 思路1 -> ... -Maximize Distance to Closest Person -```go ``` +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 +``` ## 结语 diff --git a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution.go b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution.go index d115ccf5e..736a09373 100644 --- a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution.go +++ b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(seats []int) int { + ans := 0 + pre := -1 + i := 0 + for ; i < len(seats); i++ { + s := seats[i] + if s == 0 { + continue + } + if pre == -1 { + ans = i + } else { + diff := i - pre - 1 + add := 0 + if diff&1 == 1 { + add = 1 + } + diff = diff/2 + add + ans = max(ans, diff) + } + pre = i + } + if seats[i-1] != 1 { + diff := i - pre - 1 + ans = max(ans, diff) + } + return ans } diff --git a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution_test.go b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution_test.go index 14ff50eb4..31274cd59 100644 --- a/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution_test.go +++ b/leetcode/801-900/0849.Maximize-Distance-to-Closest-Person/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 0, 0, 0, 1, 0, 1}, 2}, + {"TestCase2", []int{1, 0, 0, 0}, 3}, + {"TestCase3", []int{0, 1}, 1}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }