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

给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (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);
*/

Leetcode 304.二维区域和检索-矩阵不可变的更多相关文章

  1. Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变

    304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...

  2. LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  3. [Swift]LeetCode304. 二维区域和检索 - 矩阵不可变 | Range Sum Query 2D - Immutable

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. [Leetcode]303.区域和检索&&304.二维区域和检索

    题目 1.区域和检索: 简单题,前缀和方法 乍一看就觉得应该用前缀和来做,一个数组多次查询. 实现方法: 新建一个private数组prefix_sum[i],用来存储nums前i个数组的和, 需要找 ...

  5. 领扣(LeetCode)二维区域和检索 个人题解

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

  6. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

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

随机推荐

  1. 1-1-Java的特点

    Java语言平台 JavaSE(Java Platform Standard Edition)标准版 以前叫做J2SE,5.0版本后改名叫做JAVASE,主要用于桌面应用程序的开发,该技术体系是后两者 ...

  2. POM报错Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from

    解决方式一:   1.查看.m2\repository\org\apache\maven\plugins\maven-resources-plugin\下maven-resources-plugin- ...

  3. 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering

    http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...

  4. Oozie是什么?

         但是,一般用Azkaban了.(具体见我写的另一篇博客:) 官网:https://oozie.apache.org/ Oozie is a workflow scheduler system ...

  5. Asp.Net MVC中捕捉错误路由并设置默认Not Found页面。

    在Global中写一个Application_Error捕捉错误路由并重定向到Not Found页面.这里是全局性抓取错误路由,此处还可以写由错误路由导致访问失败的日志记录. protected vo ...

  6. C#的WinForm中Label透明一例

    很久之前開發的一個MIS系統,里面有個登錄界面,採用了PictureBox做背景,上面放了一些Label,試了很多方面不能實現透明,如下圖: 這次重新啟用該系統,看了一下原因,很簡單,原來Label的 ...

  7. slimScroll的应用(一)

    本类文章依旧是针对初学者来说的,希望大家看到后觉得有用的能给个赞~~ 什么是slimScroll? 一.官网介绍: slimScroll is a small (4.6KB) jQuery plugi ...

  8. P1303 A*B Problem

    题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式: 积 输入输出样例 输入样例#1: 1 2 输出样例#1: 2 说明 需用高精 #include<iostream> # ...

  9. sass 常用用法笔记

    最近公司开发的h5项目,需要用到sass,所以领导推荐让我去阮一峰大神的SASS用法指南博客学习,为方便以后自己使用,所以在此记录. 一.代码的重用 1.继承:SASS允许一个选择器,继承另一个选择器 ...

  10. 《Head First HTML与CSS》的CSS属性

    关于继承的结论. 1.元素选择器的作用强于继承的作用:用户定义强于浏览器默认(详见(3)<Head First HTML与CSS>学习笔记---CSS入门的2) 2.基于类的选择器> ...