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. Oracle数据库插入图片和读取图片

    package com.basicSql.scroll_page; import java.io.File; import java.io.FileInputStream; import java.i ...

  2. 9.MVC框架开发(关于ActionResult的处理)

    1.在Action处理之后,必须有一个返回值,这个返回值必须继承自ActionResult的对象 2.ActionResult,实际就是服务器端响应给客户端的结果 ViewResult(返回视图结果) ...

  3. CODEVS 1004四子连棋

    [题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...

  4. ASP.NET MVC got 405 error on HTTP DELETE request

    使用Backload的时候在本地调试通过,上传服务器后出现405错误(监控通信时可以发现ajax的返回结果为405) 通过修改webconfig可以解决: <system.webServer&g ...

  5. 简易promise

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

  6. java 发布和逸出

    [转载]:http://www.2cto.com/kf/201310/247738.html 前言 多线程并发环境下,线程安全极为重要.往往一些问题的发生都是由于不正确的发布了对象造成了对象逸出而引起 ...

  7. c++中的static关键字

    1.在函数体中,一个被声明为静态的变量在这一函数被调用的过程中维持其值不变. 2.在模块内(但在函数外),比如在某一个C源文件内,一个被声明为静态的变量可以被该模块内的所有函数调用,但不能被模块外的函 ...

  8. 详解浏览器缓存机制与Apache设置缓存

    一.详解浏览器缓存机制 对于,如何说明缓存机制,在网络上找到了两张图,个人认为思路是比较清晰的.总结时,上图. 这里需要注意的有两点: 1.Last-Modified.Etag是响应头里的数据 2.I ...

  9. [unity菜鸟] 笔记1 —— 函数篇

    SendMessage() 调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令 //①将以下函数附给target对象 void Rena ...

  10. Ubuntu10.10 安装scim

    Ubuntu10.10 上没有找到默认的输入法,所以要安装一个中文输入法,网上好多介绍的,但都 不怎么好用,下面参考http://blog.csdn.net/caodesheng110/article ...