Range Sum Query 2D - Mutable

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).

The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

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
update(3, 2, 2)
sumRegion(2, 1, 4, 3) -> 10

Note:

  1. The matrix is only modifiable by the update function.

  2. You may assume the number of calls to update and sumRegion function is distributed evenly.

  3. You may assume that row1 ≤ row2 and col1 ≤ col2.

分析:https://segmentfault.com/a/1190000004238792

注意这道题说明了"calls to update and sumRegion function is distributed evenly"。我们可以先不考虑这道题的限制,根据这个两个方法使用次数分情况讨论:

  • update多,sumRegion少
    这种情况比较简单,我们可以直接复制矩阵,update的时候直接update对应点的值即可,sumRegion直接遍历指定范围内的值就可以了。

    update: O(1), sumRegion: O(mn)
  • update少,sumRegion多。
    我们可以不复制整个矩阵,而是在每个点处存(0, 0)到该的长方形内所有元素的和,这样的话,sumRegion的复杂度会很低,update的时候我们需要update所有受影响的sum。

    update: O(mn), sumRegion: O(1)
  • update多,sumRegion多(本题情况)
    我们可以每个点存对于该行,起始点到该点为止的sum。这样话,update的话,我们只需要update该行受影响的sum即可。sumRegion的话,我们只需要遍历相应行,加上不同行的对应sum即可。

    update: O(n), sumRegion: O(m)

当然,对于这种类型的题目,使用一些高级数据结构会更时间复杂度会更低,能达到logn,如二维线段树。这里只涉及基本的数据结构,尽量不搞复杂。

复杂度:

注:m指行数,n指列数,这里global的矩阵不算各个方法的extra space。

update

time: O(n), space: O(1)

sumRegion

time: O(m), space: O(1)

 public class NumMatrix {
int[][] rowSums; public NumMatrix(int[][] matrix) {
if (matrix.length == )
return;
rowSums = new int[matrix.length][matrix[].length]; // 建rowSums矩阵
for (int i = ; i < matrix.length; i++) {
for (int j = ; j < matrix[].length; j++) {
rowSums[i][j] = matrix[i][j] + (j == ? : rowSums[i][j - ]);
}
}
} public void update(int row, int col, int val) {
// 求出新值与旧值的差
int diff = val - (rowSums[row][col] - (col == ? : rowSums[row][col - ])); // 更新该行受影响的sum
for (int j = col; j < rowSums[].length; j++) {
rowSums[row][j] += diff;
}
} public int sumRegion(int row1, int col1, int row2, int col2) {
int res = ; // 逐行求和,每行的相应和为两sum相减
for (int i = row1; i <= row2; i++) {
res += rowSums[i][col2] - (col1 == ? :rowSums[i][col1 - ]);
}
return res;
}
} // Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.update(1, 1, 10);
// numMatrix.sumRegion(1, 2, 3, 4);

Range Sum Query 2D - Mutable & Immutable

就是没有update操作,这样的话,我们直接找出0,0 到每个点的和即可。

 public class NumMatrix {
int[][] matrix; public NumMatrix(int[][] m) {
matrix = m;
if (m == null || m.length == || m[].length == ) return;
for (int i = ; i < matrix[].length; i++) {
matrix[][i] += matrix[][i - ];
} for (int i = ; i < matrix.length; i++) {
matrix[i][] += matrix[i - ][];
} for (int i = ; i < matrix.length; i++) {
for (int j = ; j < matrix[].length; j++) {
matrix[i][j] += matrix[i - ][j] + matrix[i][j - ] - matrix[i - ][j - ];
}
}
} public int sumRegion(int row1, int col1, int row2, int col2) {
if (matrix == null) return ; if (row1 == && col1 == ) return matrix[row2][col2];
if (row1 == ) return matrix[row2][col2] - matrix[row2][col1 - ];
if (col1 == ) return matrix[row2][col2] - matrix[row1 - ][col2]; return matrix[row2][col2] - matrix[row1 - ][col2] - matrix[row2][col1 - ] + matrix[row1 - ][col1 - ];
}
} // 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);

Range Sum Query 2D - Mutable & Immutable的更多相关文章

  1. [Locked] Range Sum Query 2D - Mutable

    Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...

  2. [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 ...

  3. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

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

  4. LeetCode Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  5. 308. Range Sum Query 2D - Mutable

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

  6. LeetCode 308. Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  7. [Swift]LeetCode308. 二维区域和检索 - 可变 $ Range Sum Query 2D - Mutable

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

  8. [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 ...

  9. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

随机推荐

  1. Asp.NET的Trace追踪

    http://m.blog.csdn.net/article/details?id=7026402 当我们扑捉程序错误时,调试器是开发者们最得力的助手.然而,ASP.NET的跟踪,在调试时是一个很棒的 ...

  2. Mysql InnoDB行锁实现方式(转)

    Mysql InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点 ...

  3. GC垃圾回收机制

    学习资料:http://kb.cnblogs.com/page/106720/

  4. oracle中的sql%rowcount,sql%found、sql%notfound、sql%rowcount和sql%isopen

     Oracle 存储过程 删除表记录时删除不存在的记录也是显示删除成功 create or replace procedure delDept(p_deptno in dept.deptno%type ...

  5. Apache CXF实现WebService入门教程(附完整源码)

    Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...

  6. cad中关于点样式点的绘制

    点样式 从0开始, 默认的就是0 0= 一个小点; 1= 空的, 什么都不显示; 2= +加号; 3= X 叉号 设置点样式的命令是: pdmode: 可以假设认为是: point default m ...

  7. Windows10的快捷键和新功能你利用了多少?

    win10快捷键大全大家可以来了解一下,今天小编带来了win10常用快捷键,很多朋友喜欢使用快捷键来操作电脑,那么Windows10系统有哪些新的快捷键呢• 贴靠窗口:Win +左/右> Win ...

  8. lua 学习

    尽管所有的脚本语言在特定领域都有自己的一席之地,但在游戏开发的世界里,Python 和 Lua 是非常适合的,因为它们可以直接调用C++的功能. lua最让人惊喜的地方应该是它的执行速度,目前没有任何 ...

  9. 说说移动前端中 viewport (视口)

    转载网络资源的文章:来源不详~~ 移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设 ...

  10. Linux下C语言高手成长路线(转载)

    建议学习路径: 首先先学学编辑器,vim, emacs什么的都行. 然后学make file文件,只要知道一点就行,这样就可以准备编程序了. 然后看看<C程序设计语言>K&R,这样 ...