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. Binding的源和路径

    书上写着:Binding的源也就是数据的源头.Binding对于源的要求很简单-只要他是一个对象!并且通过属性(Property)公开自己的数据,它就可以作为Binding的源了.就像上一篇我写的那个 ...

  2. iOS 浅复制、深复制、完全复制的知识点梳理验证(附加归档解档)

    在之前转载的一片文章中,文中对浅复制和深复制进行了详细的解读,同时还提到了深复制(one-level-deep copy).完全复制(true copy)的概念,并指出iOS开发中的深复制是单层深赋值 ...

  3. StringBuffer与StringBuilder原理与区别

    其实只要找下Google大神就有答案了:StringBuffer 与 StringBuilder 中的方法和功能完全是等价的,只是StringBuffer 中的方法大都采用了 synchronized ...

  4. 长达半年的苹果发布会:亮点与槽点(iPhone5s,iPhone5c)

    不知出于什么原因,今天凌晨召开的苹果发布会并没有视频直播,所以大家都守着The Verge家的图文直播.结果,苹果再一次用事实证明了他们没有保密体系,或者,故意没有保密体系. 整场发布会正经的亮点如下 ...

  5. 简易promise

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  6. 一个简单的php分页类代码(转载)

    入门级php分页类 原文地址:http://www.xfcodes.com/php/fenye/3608.htm 时间:2015-12-16 20:52:00来源:网络 php分页类. 复制代码代码如 ...

  7. hadoop-streaming 配置之---参数分割

    map: -D stream.map.output.field.separator=. 定义mapoutput字段的分隔符为. 用户可以自定义分隔符(除了默认的tab) -D stream.num.m ...

  8. ***PHP请求服务curl以及json的解析

    对于thinkphp框架,相信每一个php开发者都会有了解或者熟悉吧!前端很多都用的ajax的结合,前几天遇到了一个问题,就是请求另一个服务,也就是请求一个接口,然后对方返回一个json串,一开始对c ...

  9. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

  10. solr的原子更新/局部更新

    solr支持三种类型的原子更新: set - to set a field. add - to add to a multi-valued field. inc - to increment a fi ...