Skip to content

Commit add853b

Browse files
committed
Add solution and test-cases for proble 2536
1 parent f03226b commit add853b

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed
9.37 KB
Loading
4.16 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# [2536.Increment Submatrices by One][title]
2+
3+
## Description
4+
You are given a positive integer `n`, indicating that we initially have an `n x n` **0-indexed** integer matrix `mat` filled with zeroes.
5+
6+
You are also given a 2D integer array `query`. For each `query[i] = [row1i, col1i, row2i, col2i]`, you should do the following operation:
7+
8+
- Add `1` to **every element** in the submatrix with the **top left** corner `(row1i, col1i)` and the **bottom right** corner `(row2i, col2i)`. That is, add `1` to `mat[x][y]` for all `row1i <= x <= row2i` and `col1i <= y <= col2i`.
9+
10+
Return the matrix `mat` after performing every query.
11+
12+
**Example 1:**
13+
14+
![1](./1.png)
15+
16+
```
17+
Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]]
18+
Output: [[1,1,0],[1,2,1],[0,1,1]]
19+
Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query.
20+
- In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2).
21+
- In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).
22+
```
23+
24+
**Example 2:**
25+
26+
![2](./2.png)
27+
28+
```
29+
Input: n = 2, queries = [[0,0,1,1]]
30+
Output: [[1,1],[1,1]]
31+
Explanation: The diagram above shows the initial matrix and the matrix after the first query.
32+
- In the first query we add 1 to every element in the matrix.
33+
```
34+
35+
## 结语
36+
37+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
38+
39+
[title]: https://leetcode.com/problems/increment-submatrices-by-one
40+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, queries [][]int) [][]int {
4+
mat := make([][]int, n)
5+
for i := 0; i < n; i++ {
6+
mat[i] = make([]int, n)
7+
}
8+
for _, q := range queries {
9+
for i := q[0]; i <= q[2]; i++ {
10+
for j := q[1]; j <= q[3]; j++ {
11+
mat[i][j]++
12+
}
13+
}
14+
}
15+
return mat
516
}

leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,35 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs int
14+
queries [][]int
15+
expect [][]int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 3, [][]int{
18+
{1, 1, 2, 2}, {0, 0, 1, 1},
19+
}, [][]int{
20+
{1, 1, 0}, {1, 2, 1}, {0, 1, 1},
21+
}},
22+
{"TestCase2", 2, [][]int{{0, 0, 1, 1}}, [][]int{{1, 1}, {1, 1}}},
1923
}
2024

2125
// 开始测试
2226
for i, c := range cases {
2327
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
28+
got := Solution(c.inputs, c.queries)
2529
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
31+
c.expect, got, c.inputs, c.queries)
2832
}
2933
})
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}

0 commit comments

Comments
 (0)