Skip to content

Commit c1389e7

Browse files
committed
Add solution and test-cases for problem 764
1 parent a01e89c commit c1389e7

File tree

5 files changed

+106
-26
lines changed

5 files changed

+106
-26
lines changed
14 KB
Loading
1.26 KB
Loading

leetcode/701-800/0764.Largest-Plus-Sign/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [764.Largest Plus Sign][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+
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`.
5+
6+
Return the order of the largest **axis-aligned** plus sign of 1's contained in `grid`. If there is none, return `0`.
7+
8+
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.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./1.jpg)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: n = 5, mines = [[4,2]]
16+
Output: 2
17+
Explanation: In the above grid, the largest plus sign can only be of order 2. One of them is shown.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
22+
![2](./2.jpg)
1923

20-
### 思路1
21-
> ...
22-
Largest Plus Sign
23-
```go
2424
```
25-
25+
Input: n = 1, mines = [[0,0]]
26+
Output: 0
27+
Explanation: There is no plus sign, so return 0.
28+
```
2629

2730
## 结语
2831

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

3-
func Solution(x bool) bool {
4-
return x
3+
type l764 struct {
4+
up, down, left, right int
5+
}
6+
7+
func Solution(n int, mines [][]int) int {
8+
var ans int
9+
checker := make(map[int]map[int]struct{})
10+
for _, m := range mines {
11+
if _, ok := checker[m[0]]; !ok {
12+
checker[m[0]] = map[int]struct{}{}
13+
}
14+
checker[m[0]][m[1]] = struct{}{}
15+
}
16+
order := make([][]l764, n)
17+
for i := range n {
18+
order[i] = make([]l764, n)
19+
}
20+
21+
for i := 0; i < n; i++ {
22+
one := 0
23+
for j := 0; j < n; j++ {
24+
if v, ok := checker[i]; ok {
25+
if _, ok1 := v[j]; ok1 {
26+
one = 0
27+
continue
28+
}
29+
}
30+
order[i][j].left = one
31+
one++
32+
}
33+
one = 0
34+
for j := n - 1; j >= 0; j-- {
35+
if v, ok := checker[i]; ok {
36+
if _, ok1 := v[j]; ok1 {
37+
one = 0
38+
continue
39+
}
40+
}
41+
order[i][j].right = one
42+
one++
43+
44+
}
45+
46+
one = 0
47+
for j := 0; j < n; j++ {
48+
if v, ok := checker[j]; ok {
49+
if _, ok1 := v[i]; ok1 {
50+
one = 0
51+
continue
52+
}
53+
}
54+
order[j][i].up = one
55+
one++
56+
}
57+
one = 0
58+
for j := n - 1; j >= 0; j-- {
59+
if v, ok := checker[j]; ok {
60+
if _, ok1 := v[i]; ok1 {
61+
one = 0
62+
continue
63+
}
64+
}
65+
order[j][i].down = one
66+
one++
67+
}
68+
}
69+
70+
for i := 0; i < n; i++ {
71+
for j := 0; j < n; j++ {
72+
if v, ok := checker[i]; ok {
73+
if _, ok1 := v[j]; ok1 {
74+
continue
75+
}
76+
}
77+
78+
ans = max(ans, min(order[i][j].down, order[i][j].up, order[i][j].left, order[i][j].right)+1)
79+
}
80+
}
81+
return ans
582
}

leetcode/701-800/0764.Largest-Plus-Sign/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
mines [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, [][]int{{4, 2}}, 2},
18+
{"TestCase2", 1, [][]int{{0, 0}}, 0},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n, c.mines)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.n, c.mines)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)