Range Sum Query 2D - Immutable

Given a 2D matrix matrix[][], find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
Example:
Given matrix = [
[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]
]

sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

这道题目属于二维的矩阵运算,题目中假设矩阵没有变化,对比前面的一道题[url=http://kickcode.iteye.com/blog/2272650]Range Sum Query - Mutable[/url]来看,我们也可以用树状数组来做,这里我们就需要构建一个二维的树状数组。具体如何构建一个树状数组请看连接中的那道题。这里先给出一个AC的代码:

public class NumMatrix {
int[][] matrix;
int[][] BIT;
public NumMatrix(int[][] matrix) {
this.matrix = matrix;
if(matrix == null || matrix.length == 0 ) return;
BIT = new int[matrix.length + 1][matrix[0].length + 1];
for(int i = 0; i < matrix.length; i++)
for(int j = 0; j < matrix[0].length; j++)
init(i, j, matrix[i][j]);
}
public void init(int i, int j, int val) {
j ++;
while(j <= matrix[0].length) {
BIT[i][j] += val;
j += (j & -j);
}
}
/* implement mutable
public void update(int i, int j, int val) {
int differ = val - matrix[i][j];
matrix[i][j] = val;
init(i, j, differ);
}
*/
public int sumRegion(int row1, int col1, int row2, int col2) {
int result = 0;
for(int i = row1; i <= row2; i++) {
result += getSumRange(i, col1, col2);
}
return result;
}
public int getSumRange(int i, int start, int end) {
return getSum(i, end) - getSum(i, start - 1);
}
public int getSum(int i, int point) {
point ++;
int sum = 0;
while(point > 0) {
sum += BIT[i][point];
point -= (point & -point);
}
return sum;
}
}


// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);


代码中注释的部分是update方法,对应mutable的情况。

因为树状数组的优势在于解决对变化的数据求和,在不变的情况下,优势就没有那么明显,因为构建一个树状数组也需要消耗时间和空间。这道题我们采用动态规划的思想解决比较合适。
代码如下:

public class NumMatrix {
int[][] dp;
public NumMatrix(int[][] matrix) {
if(matrix == null || matrix.length == 0) return;
dp = new int[matrix.length + 1][matrix[0].length + 1];
for(int i = 1; i <= matrix.length; i++) {
for(int j = 1; j <= matrix[0].length; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + matrix[i - 1][j - 1] - dp[i - 1][j - 1];
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {
return dp[row2 + 1][col2 + 1] - dp[row2 + 1][col1] + dp[row1][col1] - dp[row1][col2 + 1];
}
}


// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值