Skip to content

Commit b3c1872

Browse files
authored
Merge pull request #877 from 0xff-dev/2373
Add solution and test-cases for problem 2373
2 parents f3d5710 + 33d898b commit b3c1872

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

leetcode/2301-2400/2373.Largest-Local-Values-in-a-Matrix/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2373.Largest Local Values in a Matrix][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 `n x n` integer matrix `grid`.
5+
6+
Generate an integer matrix `maxLocal` of size `(n - 2) x (n - 2)` such that:
7+
8+
- `maxLocal[i][j]` is equal to the `largest` value of the `3 x 3` matrix in `grid` centered around row `i + 1` and column `j + 1`.
9+
In other words, we want to find the largest value in every contiguous `3 x 3` matrix in `grid`.
10+
11+
Return the generated matrix.
712

8-
**Example 1:**
13+
**Example 1:**
14+
15+
![1](./ex1.png)
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
19+
Output: [[9,9],[8,6]]
20+
Explanation: The diagram above shows the original matrix and the generated matrix.
21+
Notice that each value in the generated matrix corresponds to the largest value of a contiguous 3 x 3 matrix in grid.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
26+
![2](./ex2new2.png)
1927

20-
### 思路1
21-
> ...
22-
Largest Local Values in a Matrix
23-
```go
2428
```
25-
29+
Input: grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
30+
Output: [[2,2,2],[2,2,2],[2,2,2]]
31+
Explanation: Notice that the 2 is contained within every contiguous 3 x 3 matrix in grid.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) [][]int {
4+
rows, cols := len(grid), len(grid[0])
5+
ans := make([][]int, rows-2)
6+
for i := 0; i < rows-2; i++ {
7+
ans[i] = make([]int, cols-2)
8+
}
9+
for r := 1; r < rows-1; r++ {
10+
for c := 1; c < cols-1; c++ {
11+
cur := grid[r][c]
12+
cur = max(cur, grid[r-1][c-1])
13+
cur = max(cur, grid[r-1][c])
14+
cur = max(cur, grid[r-1][c+1])
15+
cur = max(cur, grid[r][c-1])
16+
cur = max(cur, grid[r][c+1])
17+
cur = max(cur, grid[r+1][c-1])
18+
cur = max(cur, grid[r+1][c])
19+
cur = max(cur, grid[r+1][c+1])
20+
ans[r-1][c-1] = cur
21+
}
22+
}
23+
24+
return ans
525
}

leetcode/2301-2400/2373.Largest-Local-Values-in-a-Matrix/Solution_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ 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{
17+
{9, 9, 8, 1}, {5, 6, 2, 6}, {8, 2, 6, 4}, {6, 2, 2, 2},
18+
}, [][]int{{9, 9}, {8, 6}}},
19+
{"TestCase2", [][]int{
20+
{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1},
21+
{1, 1, 2, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1},
22+
}, [][]int{
23+
{2, 2, 2}, {2, 2, 2}, {2, 2, 2},
24+
}},
1925
}
2026

2127
// 开始测试
@@ -30,10 +36,10 @@ func TestSolution(t *testing.T) {
3036
}
3137
}
3238

33-
// 压力测试
39+
// 压力测试
3440
func BenchmarkSolution(b *testing.B) {
3541
}
3642

37-
// 使用案列
43+
// 使用案列
3844
func ExampleSolution() {
3945
}
Loading
Loading

0 commit comments

Comments
 (0)