Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions leetcode/1101-1200/1139.Largest-1-Bordered-Square/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
58 changes: 56 additions & 2 deletions leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading