diff --git a/leetcode/1101-1200/1139.Largest-1-Bordered-Square/README.md b/leetcode/1101-1200/1139.Largest-1-Bordered-Square/README.md index 39fa72025..f5c677998 100644 --- a/leetcode/1101-1200/1139.Largest-1-Bordered-Square/README.md +++ b/leetcode/1101-1200/1139.Largest-1-Bordered-Square/README.md @@ -1,28 +1,21 @@ # [1139.Largest 1-Bordered Square][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 +Given a 2D `grid` of `0`s and `1`s, return the number of elements in the largest **square** subgrid that has all `1`s on its **border**, or 0 if such a subgrid doesn't exist in the `grid`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: grid = [[1,1,1],[1,0,1],[1,1,1]] +Output: 9 ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Largest 1-Bordered Square -```go ``` - +Input: grid = [[1,1,0,0]] +Output: 1 +``` ## 结语 diff --git a/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution.go b/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution.go index d115ccf5e..b4a947b1f 100644 --- a/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution.go +++ b/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution.go @@ -1,5 +1,59 @@ package Solution -func Solution(x bool) bool { - return x +type cell struct { + left, up int +} + +func Solution(grid [][]int) int { + ans := 0 + rows, cols := len(grid), len(grid[0]) + count := make([][]cell, rows) + for i := range rows { + count[i] = make([]cell, cols) + } + for i := 0; i < rows; i++ { + pre := 0 + for j := 0; j < cols; j++ { + if grid[i][j] == 1 { + pre++ + } else { + pre = 0 + } + count[i][j].left = pre + } + } + for j := 0; j < cols; j++ { + pre := 0 + for i := 0; i < rows; i++ { + if grid[i][j] == 1 { + pre++ + } else { + pre = 0 + } + count[i][j].up = pre + } + } + for i := 0; i < rows; i++ { + for j := 0; j < cols; j++ { + if grid[i][j] == 0 { + continue + } + ans = max(ans, 1) + for l := 2; i+l-1 < rows && j+l-1 < cols; l++ { + x, y := i+l-1, j+l-1 + if grid[x][y] == 0 { + continue + } + + left := count[x][j].up - count[i][j].up + 1 + bottom := count[x][y].left - count[x][j].left + 1 + right := count[x][y].up - count[i][y].up + 1 + top := count[i][y].left - count[i][j].left + 1 + if left == l && bottom == l && right == l && top == l { + ans = max(ans, l) + } + } + } + } + return ans * ans } diff --git a/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution_test.go b/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution_test.go index 14ff50eb4..74453f8f7 100644 --- a/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution_test.go +++ b/leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution_test.go @@ -10,12 +10,11 @@ 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, 1, 1}, {1, 0, 1}, {1, 1, 1}}, 9}, + {"TestCase2", [][]int{{1, 1}, {0, 0}}, 1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }