[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 ...
随机推荐
- 【BZOJ】1006: [HNOI2008]神奇的国度 弦图消除完美序列问题
1006: [HNOI2008]神奇的国度 Description K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则. 他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的 ...
- Hive优化(转)
一.join优化 Join查找操作的基本原则:应该将条目少的表/子查询放在 Join 操作符的左边.原因是在 Join 操作的 Reduce 阶段,位于 Join 操作符左边的表的内容会被加载进内存, ...
- Appdelegate 导航操作
隐藏返回按钮 self.navigationItem.hidesBackButton = YES; 设置导航的透明度 self.navigationController.navigationBar.t ...
- sencha architect/sencha touch , to prevent breakpoint lost when you debug
add this to your loader config: Ext.Loader.setConfig({ disableCaching: false }); or : click 'applica ...
- Mac OS系统 - 将视频转换成gif
github中开源轻量级应用:droptogif
- CSS3实战:让我们尽情的圆角吧
如果说,WAP2.0网页的机型.浏览器适配给我们无线制作经理造成了巨大的心理 阴影,那么从iPhone.Android这些高端手机应用 起,我们终于可以庆幸比其他同行提 前迎来了一个新时代,这两种高端 ...
- 为你下一个项目准备的 50 个 Bootstrap 插件
Bootstrap是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 本文向你 ...
- 浏览我的php网页时,出现的都是网页的代码
添加php模块 ,在apache/conf/httpd.conf,如果是windows下的话,添加如下代码,具体路径你根据具体情况设置#BEGIN PHP INSTALLER EDITS - REMO ...
- [状压dp]HDOJ1565 方格取数(1)
中文题~~ 题意略 $n\le 20$ ! 很明显是状压! #include <cstdio> #include <cstdlib> #include <cstring& ...
- 李洪强漫谈iOS开发[C语言-016]-变量的作用域