diff --git a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/1.png b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/1.png new file mode 100644 index 000000000..e16e639bf Binary files /dev/null and b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/1.png differ diff --git a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/2.png b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/2.png new file mode 100644 index 000000000..73cca17f3 Binary files /dev/null and b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/2.png differ diff --git a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/README.md b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/README.md new file mode 100644 index 000000000..6db7e23e9 --- /dev/null +++ b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/README.md @@ -0,0 +1,40 @@ +# [2536.Increment Submatrices by One][title] + +## Description +You are given a positive integer `n`, indicating that we initially have an `n x n` **0-indexed** integer matrix `mat` filled with zeroes. + +You are also given a 2D integer array `query`. For each `query[i] = [row1i, col1i, row2i, col2i]`, you should do the following operation: + +- 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`. + +Return the matrix `mat` after performing every query. + +**Example 1:** + +![1](./1.png) + +``` +Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]] +Output: [[1,1,0],[1,2,1],[0,1,1]] +Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. +- 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). +- 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). +``` + +**Example 2:** + +![2](./2.png) + +``` +Input: n = 2, queries = [[0,0,1,1]] +Output: [[1,1],[1,1]] +Explanation: The diagram above shows the initial matrix and the matrix after the first query. +- In the first query we add 1 to every element in the matrix. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/increment-submatrices-by-one +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution.go b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution.go index d115ccf5e..4e3fdda11 100755 --- a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution.go +++ b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, queries [][]int) [][]int { + mat := make([][]int, n) + for i := 0; i < n; i++ { + mat[i] = make([]int, n) + } + for _, q := range queries { + for i := q[0]; i <= q[2]; i++ { + for j := q[1]; j <= q[3]; j++ { + mat[i][j]++ + } + } + } + return mat } diff --git a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution_test.go b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution_test.go index 14ff50eb4..63cf3703e 100755 --- a/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution_test.go +++ b/leetcode/2501-2600/2536.Increment-Submatrices-by-One/Solution_test.go @@ -9,31 +9,35 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + inputs int + queries [][]int + expect [][]int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, [][]int{ + {1, 1, 2, 2}, {0, 0, 1, 1}, + }, [][]int{ + {1, 1, 0}, {1, 2, 1}, {0, 1, 1}, + }}, + {"TestCase2", 2, [][]int{{0, 0, 1, 1}}, [][]int{{1, 1}, {1, 1}}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.queries) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }