Skip to content

Commit c936d1f

Browse files
authored
Merge pull request #1310 from 0xff-dev/1139
Add solution and test-cases for problem 1139
2 parents 9c4fb2d + d4833e8 commit c936d1f

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

leetcode/1101-1200/1139.Largest-1-Bordered-Square/README.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
# [1139.Largest 1-Bordered Square][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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`.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
10+
Output: 9
1311
```
1412

15-
## 题意
16-
> ...
17-
18-
## 题解
13+
**Example 2:**
1914

20-
### 思路1
21-
> ...
22-
Largest 1-Bordered Square
23-
```go
2415
```
25-
16+
Input: grid = [[1,1,0,0]]
17+
Output: 1
18+
```
2619

2720
## 结语
2821

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type cell struct {
4+
left, up int
5+
}
6+
7+
func Solution(grid [][]int) int {
8+
ans := 0
9+
rows, cols := len(grid), len(grid[0])
10+
count := make([][]cell, rows)
11+
for i := range rows {
12+
count[i] = make([]cell, cols)
13+
}
14+
for i := 0; i < rows; i++ {
15+
pre := 0
16+
for j := 0; j < cols; j++ {
17+
if grid[i][j] == 1 {
18+
pre++
19+
} else {
20+
pre = 0
21+
}
22+
count[i][j].left = pre
23+
}
24+
}
25+
for j := 0; j < cols; j++ {
26+
pre := 0
27+
for i := 0; i < rows; i++ {
28+
if grid[i][j] == 1 {
29+
pre++
30+
} else {
31+
pre = 0
32+
}
33+
count[i][j].up = pre
34+
}
35+
}
36+
for i := 0; i < rows; i++ {
37+
for j := 0; j < cols; j++ {
38+
if grid[i][j] == 0 {
39+
continue
40+
}
41+
ans = max(ans, 1)
42+
for l := 2; i+l-1 < rows && j+l-1 < cols; l++ {
43+
x, y := i+l-1, j+l-1
44+
if grid[x][y] == 0 {
45+
continue
46+
}
47+
48+
left := count[x][j].up - count[i][j].up + 1
49+
bottom := count[x][y].left - count[x][j].left + 1
50+
right := count[x][y].up - count[i][y].up + 1
51+
top := count[i][y].left - count[i][j].left + 1
52+
if left == l && bottom == l && right == l && top == l {
53+
ans = max(ans, l)
54+
}
55+
}
56+
}
57+
}
58+
return ans * ans
559
}

leetcode/1101-1200/1139.Largest-1-Bordered-Square/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, 9},
17+
{"TestCase2", [][]int{{1, 1}, {0, 0}}, 1},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)