[Locked] Range Sum Query 2D - Mutable
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.
分析:
如果不考虑第二个条件,即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的更多相关文章
- 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 ...
- [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 ...
随机推荐
- 仿今日头条最强顶部导航指示器,支持6种模式-b
项目中经常会用到类似今日头条中顶部的导航指示器,我也经常用一个类似的库PagerSlidingTabStrip,但是有时并不能小伙伴们的所有需求,所以我在这个类的基础上就所有能用到的情况做了一个简单的 ...
- jquery and event
jquery阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; jquery阻止默认操作 event.preventDefault() ...
- 在eclipse中将项目发布到tomcat的root目录
(1)设置项目上下文,右击项目-properties >Web Page Edit
- 面向XX编程
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/5033186.html ] 基于面向XX编程的个人理解 面向过程编程 Procedure Or ...
- 关于CPU亲和性的测试
今天看到运维的同事在配置nginx的CPU亲和性时候,运维同事说他在所有的机器上都是按照8核的方式来配置worker进程的CPU亲和性的. 但我觉得就是有点不太对劲,就查了一下nginx的处理work ...
- 同一客户代码下一个CPN对应多个FG会出现CPN描述一样的问题
Q&A : D2E01客户下已经存在CPN: TP1062/A 对应FG: P5M2812512AFHHF0 直接在客户代码下无法新建,只能在交叉信息新建CPN: TP1062/A, 但无法修 ...
- Oracle系列之异常处理
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 使用select into语句读取tb_Employee的一行,使用异常处理处理no_data_found和two_many_rows ...
- hdu4734F(x)(dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4734 各种不细心啊 居然算的所有和最大值会小于1024... 第二次做数位DP 不是太熟 #include ...
- hadoop异常:java.lang.RuntimeException: java.lang.NoSuchMethodException
出现异常的程序大致框架是这样的: public class getMaxTemperature extends Configured implements Tool { ... class MaxTe ...
- [LOJ 1008] Fibsieve`s Fantabulous Birthday
A - Fibsieve`s Fantabulous Birthday Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%l ...