Range Sum Query 2D - Mutable & Immutable
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:
The matrix is only modifiable by the update function.
You may assume the number of calls to update and sumRegion function is distributed evenly.
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的更多相关文章
- [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 ...
 - [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 ...
 - 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 ...
 - LeetCode Range Sum Query 2D - Mutable
		
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
 - 308.	Range Sum Query 2D - Mutable
		
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
 - LeetCode 308. Range Sum Query 2D - Mutable
		
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
 - [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 ...
 - [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 ...
 - [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 ...
 
随机推荐
- OC-改错题
			
1,类方法中不能访问成员变量 2,id后不能加*(因为id相当于NSObject *) 3,id类型的变量不能用点语法 4,类对象只能调用类方法,不能调用对象方法 .description #impo ...
 - django_web代码更新
 - idea中的svn配置
			
idea的使用之svn篇--有图超详细 http://ylq365.iteye.com/blog/1955291
 - 几种 Java 序列化方案的性能比较
			
较结果: create ser deser total size +dfl java-built-in 62 5608 29649 35257 889 514 hessian 65 3812 6708 ...
 - JQuery Easy Ui dataGrid 数据表格
			
数据表格 - DataGrid 英文文档:http://www.jeasyui.com/documentation/index.php# 继承$.fn.panel.defaults,使用$.fn.da ...
 - Xcode常用技巧(2)-使Xcode在创建类时自动添加前缀
			
在Xcode5之前的版本中,Xcode在新建项目时,会要求为一个类指定一个前缀,这样方便我们区分相同名字的类.而从Xcode6开始,由于Swift增加了命名空间的关系,Xcode在新建项目时,不会再要 ...
 - [Angularjs]ng-include 包含
			
写在前面 有时我们需要动态的创建一些标签,或者在收到服务端返回的json,创建一些标签然后找到页面上的元素,通过innerHTML写入到页面上面.angularjs也为我们提供了一种比较方便操作方式, ...
 - C语言之strrchr函数
			
from:http://blog.csdn.net/hgj125073/article/details/8443912 [FROM MSDN && 百科] 原型:char *strrc ...
 - 【10-25】OOP基础-飞机游戏知识点
			
知识点 鼠标适配器类为抽象类,使用匿名子类实现鼠标事件的重写,创建一个鼠标适配器对象 添加鼠标事件监听器到JPanel对象实现鼠标的响应 创建定时器Timer对象,在定时器的任务列表方法schedul ...
 - Entity Framework实例详解
			
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...