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. dns泛解析漫谈

    比如说:http://www.aaa.com/ 指向10.10.1.1,ftp.aaa.com/ 指向10.10.2.2,如果这时候客户访问的是aaa.com或者error.aaa.com (这里er ...

  2. SCWS分词扩展在WINDOWS下的安装方法

    安装之前先确认您是否拥有主机的安装权限,否则无法进行安装,安装步骤如下: 1. 根据您当前用的 PHP 版本,下载相应已编译好的 php_scws.dll 扩展库. 目前支持以下版本   [PHP-4 ...

  3. SQL JOINS

  4. 浅谈T-SQL中的联接查询

    引言 平时开发时,经常会使用数据库进行增删改查,免不了会涉及多表联接.今天就简单的记录下T-SQL下的联接操作. 联接类型及其介绍 在T-SQL中联接操作使用的是JOIN表运算符.联接有三种基本的类型 ...

  5. C# 拷贝目录

    public class DirectoryExtends { /// <summary> /// 拷贝目录 /// </summary> /// <param name ...

  6. [转] 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2出现0x80072f8a未指定的错误

    原文地址:安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2出现0x80072f8a未指定的错误 最近DotNetCore更新到了1.0.1,Azure tools ...

  7. SVN使用教程总结[转]

    SVN使用教程总结   SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Sub ...

  8. SQL like 模糊查询

    SQL 模糊查询 在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: ...

  9. solr6.1-----solrJ 程序管理索引库

    solrJ 是solr 提供的一个客户端,就是一个jar 包,把jar 添加到工程中整合solr 服务. 所需jar 包 D:\solr-6.1.0\dist 下面的 solr-solrj-6.1.0 ...

  10. 大熊君大话NodeJS之------Buffer模块

    一,开篇分析 所谓缓冲区Buffer,就是 "临时存贮区" 的意思,是暂时存放输入输出数据的一段内存. JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一 ...