二维区域和检索 - 矩阵不可变

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。


上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。

示例:

给定 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

说明:

  1. 你可以假设矩阵不可变。
  2. 会多次调用 sumRegion 方法
  3. 你可以假设 row1 ≤ row2 且 col1 ≤ col2。

Approach #4 (Caching Smarter) [Accepted]

Algorithm

We used a cumulative sum array in the 1D version. We notice that the cumulative sum is computed with respect to the origin at index 0. Extending this analogy to the 2D case, we could pre-compute a cumulative region sum with respect to the origin at (0,0)(0, 0)(0,0).


Sum(OD) is the cumulative region sum with respect to the origin at (0, 0).

How do we derive Sum(ABCD)Sum(ABCD)Sum(ABCD) using the pre-computed cumulative region sum?


Sum(OB) is the cumulative region sum on top of the rectangle.


Sum(OC) is the cumulative region sum to the left of the rectangle.


Sum(OA) is the cumulative region sum to the top left corner of the rectangle.

Note that the region Sum(OA)Sum(OA)Sum(OA) is covered twice by both Sum(OB)Sum(OB)Sum(OB) and Sum(OC)Sum(OC)Sum(OC). We could use the principle of inclusion-exclusion to calculate Sum(ABCD)Sum(ABCD)Sum(ABCD) as following:

Sum(ABCD)=Sum(OD)−Sum(OB)−Sum(OC)+Sum(OA) Sum(ABCD) = Sum(OD) - Sum(OB) - Sum(OC) + Sum(OA) Sum(ABCD)=Sum(OD)−Sum(OB)−Sum(OC)+Sum(OA)

 class NumMatrix {

     private int[][] dp;

     public NumMatrix(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return;
dp = new int[matrix.length + 1][matrix[0].length + 1];
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[0].length; c++) {
dp[r + 1][c + 1] = dp[r + 1][c] + dp[r][c + 1] + matrix[r][c] - dp[r][c];
}
}
} public int sumRegion(int row1, int col1, int row2, int col2) {
return dp[row2 + 1][col2 + 1] - dp[row1][col2 + 1] - dp[row2 + 1][col1] + dp[row1][col1];
} } /**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/

最新文章

  1. Linux基础介绍【第九篇】
  2. Android内存清理
  3. iOS Swift-注释与分号
  4. 纸上谈兵:表(list)
  5. 【原创】C#搭建足球赛事资料库与预测平台(3) 基础数据表设计
  6. SQL中批量删除被注入的恶意代码的方法
  7. 使用swiper和吸顶效果代码
  8. SQL加、查、改、删、函数
  9. HDU 2859 Phalanx (dp)
  10. DTD与XSD的区别
  11. linux禁ping和允许ping的方法
  12. nefu 1029 字符串
  13. SQL Server 2008R2的安装
  14. git添加本地仓库与远程仓库连接
  15. Android的Spinner控件用法解析
  16. Linux 学习笔记_12_文件共享服务_2_FTP应用--vsftpd
  17. 加载xib文件,如果想在初始化的时候就添加点东西就重载-(id)initWithCoder:(NSCoder *)aDecoder
  18. Scala学习(九)---文件和正则表达式
  19. 我的微信小程序第三篇(app.json)
  20. P66 整环的零元

热门文章

  1. Android应用的安全隐患*
  2. 数据库SQL server 删除一张表中的重复记录
  3. mac及windows下安装ss实现***
  4. 关于 propertychange 兼容性问题
  5. iptables规则的关系
  6. 如何修改开发板主频--迅为iMX6UL开发板
  7. swift 泛型的类型约束
  8. iptables 防火墙
  9. webpack、node、npm之间的关系
  10. 【转】解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)