-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path304-RangeSumQuery2D-Immutable.go
More file actions
89 lines (78 loc) · 2.96 KB
/
304-RangeSumQuery2D-Immutable.go
File metadata and controls
89 lines (78 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import "fmt"
// 304. Range Sum Query 2D - Immutable
// Given a 2D matrix matrix, handle multiple queries of the following type:
// Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
// Implement the NumMatrix class:
// NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
// int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
// You must design an algorithm where sumRegion works on O(1) time complexity.
// Example 1:
// <img src="https://pic.leetcode-cn.com/1626332422-wUpUHT-image.png" />
// Input
// ["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
// [
// [
// [
// [3, 0, 1, 4, 2],
// [5, 6, 3, 2, 1],
// [1, 2, 0, 1, 5],
// [4, 1, 0, 1, 7],
// [1, 0, 3, 0, 5]
// ]
// ],
// [2, 1, 4, 3],
// [1, 1, 2, 2],
// [1, 2, 2, 4]
// ]
// Output
// [null, 8, 11, 12]
// Explanation
// NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]);
// numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
// numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
// numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
// Constraints:
// m == matrix.length
// n == matrix[i].length
// 1 <= m, n <= 200
// -10^4 <= matrix[i][j] <= 10^4
// 0 <= row1 <= row2 < m
// 0 <= col1 <= col2 < n
// At most 10^4 calls will be made to sumRegion.
// f(i, j) = f(i-1, j) + f(i, j-1) - f(i-1, j-1) + matrix[i][j]
type NumMatrix struct {
cumsum [][]int
}
func Constructor(matrix [][]int) NumMatrix {
if len(matrix) == 0 {
return NumMatrix{nil}
}
// 生成一个二维数组保存结果
cumsum := make([][]int, len(matrix)+1)
cumsum[0] = make([]int, len(matrix[0])+1)
for i := range matrix {
cumsum[i+1] = make([]int, len(matrix[i])+1)
for j := range matrix[i] {
cumsum[i+1][j+1] = matrix[i][j] + cumsum[i][j+1] + cumsum[i+1][j] - cumsum[i][j]
}
}
fmt.Println(cumsum)
return NumMatrix{cumsum}
}
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
cumsum := this.cumsum
return cumsum[row2+1][col2+1] - cumsum[row1][col2+1] - cumsum[row2+1][col1] + cumsum[row1][col1]
}
func main() {
numMatrix := Constructor([][]int{
{3, 0, 1, 4, 2},
{5, 6, 3, 2, 1},
{1, 2, 0, 1, 5},
{4, 1, 0, 1, 7},
{1, 0, 3, 0, 5},
})
fmt.Println(numMatrix.SumRegion(2, 1, 4, 3)); // return 8 (i.e sum of the red rectangle)
fmt.Println(numMatrix.SumRegion(1, 1, 2, 2)); // return 11 (i.e sum of the green rectangle)
fmt.Println(numMatrix.SumRegion(1, 2, 2, 4)); // return 12 (i.e sum of the blue rectangle)
}