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.

分析:

  如果不考虑第二个条件,即update和sumRegion分布均匀,则该题有三种情况。

  1. update少,sumRegion多,则关键简化sumRegion步骤。

    对于sumRegion,使用(0, 0)到(i, j)的矩阵和作为基本存储元素sum[i][j],利用矩阵的重叠关系,有sum[m ~ n][p ~ q] = sum[n][q] - sum[n][p - 1] - sum[m - 1][q] + sum[m - 1][p - 1]。复杂度为O(1)。

    对于update,需要更新所有的sum[i][j]。复杂度为O(MN),M,N为矩阵长宽。

  2. update多,sumRegion少,则关键简化update步骤。

    对于update,更新当前位置即可。复杂度为O(1)。

    对于sumRegion,一个元素一个元素地累加。复杂度为O(MN)。

  3. update多,sumRegion多,需要折衷方案。

    对于sumRegion,既不一个个加,也不矩阵加减,而是一行行加。复杂度为O(M)。

    对于update,需要计算一行行的(row, 0)到(row, j)的值rowsum[row][j]。复杂度为O(N)。

    对于3,其实可以用线段树降到log复杂度。

代码:

class Solution {
private:
vector<vector<int> > rowsum;
public:
Solution(vector<vector<int> > matrix) {
for(auto row : matrix) {
vector<int> srs(, );
int count = ;
for(int val : row)
srs.push_back(count += val);
rowsum.push_back(srs);
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
int sum = ;
for(int i = row1; i <= row2; i++)
sum += rowsum[i][col2 + ] - rowsum[i][col1];
return sum;
}
void update(int row, int col, int val) {
int diff = val - (rowsum[row][col + ] - rowsum[row][col]);
for(int j = col + ; j < rowsum[].size(); j++)
rowsum[row][j] += diff;
return;
}
};

[Locked] Range Sum Query 2D - Mutable的更多相关文章

  1. Range Sum Query 2D - Mutable & Immutable

    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. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  2. vim file save as

    the command of vim to save as the file :w new_file_name

  3. uCGUI动态内存管理

    动态内存的堆区 /* 堆区共用体定义 */ typedef union { /* 可以以4字节来访问堆区,也可以以1个字节来访问 */ ]; /* required for proper aligne ...

  4. IOS 获得通讯录中联系人的所有属性 备用参考

    ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...

  5. python 时间及日期函数

    本人最近新学python ,用到关于时间和日期的函数,经过一番研究,从网上查找资料,经过测试,总结了一下相关的方法. import timeimport datetime '''时间转化为时间戳: 2 ...

  6. xrange和range区别

    range和xrange这两个函数基本都是在循环的时候使用的. >>> for x in range(10,21,1): ... print x ... 10 11 12 13 14 ...

  7. UPUPW PHP环境集成包

    UPUPW PHP环境集成包 http://www.upupw.net/

  8. 练习C之SELECT形式的非阻塞IO

    呵呵,理解得不深,但毕竟手打全版,且无错.但select.h不知何处找头文件, 粘下来作个记录. POLL,EPOLL感觉代码类似,只是函数和系统实现不一样,,EPOLL目前最合理的.定位精确,算法复 ...

  9. php重定向 htaccess文件的编写

    主页的url重写规则:/controller/action.html(其中第一个英文代表控制器,第二个英文代表动作,映射到:index.php?c=controller&a=action) 后 ...

  10. Linux下打包压缩成war包和解压war包

    一. 打包成war包 因为种种原因公司需要把java程序达成war包.起先用zip命令打包,起先可以用,后来却无法使用.今天找到一个更好的办法.用jar命令,前提是要安装jdk. 把当前目录下的所有文 ...