Skip to content

Commit 8edc870

Browse files
committed
Merge pull request #209 from 0xff-dev/master
Add solution and test-cases for problem 1314
2 parents a3f0694 + cf58268 commit 8edc870

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

leetcode/1301-1400/1314.Matrix-Block-Sum/README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# [1314.Matrix Block Sum][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 `m x n` matrix `mat` and an integer `k`, return a matrix `answer` where each `answer[i][j]` is the sum of all elements `mat[r][c]` for:
5+
6+
- `i - k <= r <= i + k,`
7+
- `j - k <= c <= j + k,` and
8+
- `(r, c)` is a valid position in the matrix.
9+
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
15+
Output: [[12,21,16],[27,45,33],[24,39,28]]
1316
```
1417

15-
## 题意
16-
> ...
18+
__Example 2:__
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Matrix Block Sum
23-
```go
20+
```
21+
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
22+
Output: [[45,45,45],[45,45,45],[45,45,45]]
2423
```
2524

2625

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(mat [][]int, k int) [][]int {
4+
m, n := len(mat), len(mat[0])
5+
cols := make([]int, n)
6+
rows := make([]int, m)
7+
for col := 0; col < n; col++ {
8+
rows[0] = mat[0][col]
9+
for row := 1; row < m; row++ {
10+
mat[row][col] += mat[row-1][col]
11+
rows[row] = mat[row][col]
12+
}
13+
14+
mat[0][col] = mat[m-1][col]
15+
if k < m {
16+
mat[0][col] = mat[k][col]
17+
}
18+
for row := 1; row < m; row++ {
19+
end, start := row+k, row-k-1
20+
if end >= m {
21+
end = m - 1
22+
}
23+
24+
mat[row][col] = rows[end]
25+
if start >= 0 {
26+
mat[row][col] -= rows[start]
27+
}
28+
}
29+
}
30+
31+
for row := 0; row < m; row++ {
32+
cols[0] = mat[row][0]
33+
for col := 1; col < n; col++ {
34+
mat[row][col] += mat[row][col-1]
35+
cols[col] = mat[row][col]
36+
}
37+
38+
mat[row][0] = mat[row][n-1]
39+
if k < n {
40+
mat[row][0] = mat[row][k]
41+
}
42+
for col := 1; col < n; col++ {
43+
left, right := col-k-1, col+k
44+
if right >= n {
45+
right = n - 1
46+
}
47+
mat[row][col] = cols[right]
48+
if left >= 0 {
49+
mat[row][col] -= cols[left]
50+
}
51+
}
52+
}
53+
54+
return mat
555
}

leetcode/1301-1400/1314.Matrix-Block-Sum/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
matrix [][]int
14+
k int
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 1, [][]int{{12, 21, 16}, {27, 45, 33}, {24, 39, 28}}},
18+
{"TestCase2", [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 2, [][]int{{45, 45, 45}, {45, 45, 45}, {45, 45, 45}}},
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.matrix, c.k)
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 input-mat: %v, input-k: %v",
27+
c.expect, got, c.matrix, c.k)
2828
}
2929
})
3030
}

0 commit comments

Comments
 (0)