diff --git a/leetcode/701-800/0764.Largest-Plus-Sign/1.jpg b/leetcode/701-800/0764.Largest-Plus-Sign/1.jpg new file mode 100644 index 000000000..af33484e7 Binary files /dev/null and b/leetcode/701-800/0764.Largest-Plus-Sign/1.jpg differ diff --git a/leetcode/701-800/0764.Largest-Plus-Sign/2.jpg b/leetcode/701-800/0764.Largest-Plus-Sign/2.jpg new file mode 100644 index 000000000..7d63b9ac4 Binary files /dev/null and b/leetcode/701-800/0764.Largest-Plus-Sign/2.jpg differ diff --git a/leetcode/701-800/0764.Largest-Plus-Sign/README.md b/leetcode/701-800/0764.Largest-Plus-Sign/README.md index 9e5a1d57e..93368930b 100644 --- a/leetcode/701-800/0764.Largest-Plus-Sign/README.md +++ b/leetcode/701-800/0764.Largest-Plus-Sign/README.md @@ -1,28 +1,31 @@ # [764.Largest Plus Sign][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 integer `n`. You have an `n x n` binary `grid` grid with all values initially `1`'s except for some indices given in the array `mines`. The `ith` element of the array `mines` is defined as `mines[i] = [xi, yi]` where `grid[xi][yi] == 0`. + +Return the order of the largest **axis-aligned** plus sign of 1's contained in `grid`. If there is none, return `0`. + +An **axis-aligned** plus sign of `1`'s of order `k` has some center `grid[r][c] == 1` along with four arms of length `k - 1` going up, down, left, and right, and made of `1`'s. Note that there could be `0`'s or `1`'s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for `1`'s. -**Example 1:** +**Example 1:** + +![1](./1.jpg) ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 5, mines = [[4,2]] +Output: 2 +Explanation: In the above grid, the largest plus sign can only be of order 2. One of them is shown. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.jpg) -### 思路1 -> ... -Largest Plus Sign -```go ``` - +Input: n = 1, mines = [[0,0]] +Output: 0 +Explanation: There is no plus sign, so return 0. +``` ## 结语 diff --git a/leetcode/701-800/0764.Largest-Plus-Sign/Solution.go b/leetcode/701-800/0764.Largest-Plus-Sign/Solution.go index d115ccf5e..fda3a5a04 100644 --- a/leetcode/701-800/0764.Largest-Plus-Sign/Solution.go +++ b/leetcode/701-800/0764.Largest-Plus-Sign/Solution.go @@ -1,5 +1,82 @@ package Solution -func Solution(x bool) bool { - return x +type l764 struct { + up, down, left, right int +} + +func Solution(n int, mines [][]int) int { + var ans int + checker := make(map[int]map[int]struct{}) + for _, m := range mines { + if _, ok := checker[m[0]]; !ok { + checker[m[0]] = map[int]struct{}{} + } + checker[m[0]][m[1]] = struct{}{} + } + order := make([][]l764, n) + for i := range n { + order[i] = make([]l764, n) + } + + for i := 0; i < n; i++ { + one := 0 + for j := 0; j < n; j++ { + if v, ok := checker[i]; ok { + if _, ok1 := v[j]; ok1 { + one = 0 + continue + } + } + order[i][j].left = one + one++ + } + one = 0 + for j := n - 1; j >= 0; j-- { + if v, ok := checker[i]; ok { + if _, ok1 := v[j]; ok1 { + one = 0 + continue + } + } + order[i][j].right = one + one++ + + } + + one = 0 + for j := 0; j < n; j++ { + if v, ok := checker[j]; ok { + if _, ok1 := v[i]; ok1 { + one = 0 + continue + } + } + order[j][i].up = one + one++ + } + one = 0 + for j := n - 1; j >= 0; j-- { + if v, ok := checker[j]; ok { + if _, ok1 := v[i]; ok1 { + one = 0 + continue + } + } + order[j][i].down = one + one++ + } + } + + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + if v, ok := checker[i]; ok { + if _, ok1 := v[j]; ok1 { + continue + } + } + + ans = max(ans, min(order[i][j].down, order[i][j].up, order[i][j].left, order[i][j].right)+1) + } + } + return ans } diff --git a/leetcode/701-800/0764.Largest-Plus-Sign/Solution_test.go b/leetcode/701-800/0764.Largest-Plus-Sign/Solution_test.go index 14ff50eb4..8d7e852e4 100644 --- a/leetcode/701-800/0764.Largest-Plus-Sign/Solution_test.go +++ b/leetcode/701-800/0764.Largest-Plus-Sign/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n int + mines [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, [][]int{{4, 2}}, 2}, + {"TestCase2", 1, [][]int{{0, 0}}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.mines) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.mines) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }