Range Sum Query 2d-mutable & Immutable

Source: Internet
Author: User

Range Sum Query 2d-mutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by it upper left corner (Row1, col1) and lower right corner (Row2, col2).

The above rectangle (with the red border) are defined by (row1, col1) = (2, 1) and (Row2, col2) = (4, 3), which contains Su m = 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), 8update (3, 2,  2) sumregion (2, 1, 4, 3), 10          

Note:

    1. The matrix is a modifiable by the update function.

    2. Assume the number of calls to update and sumregion function is distributed evenly.

    3. Assume that Row1≤row2 and col1≤col2.

Analysis: 1190000004238792

Notice the problem "calls to update and sumRegion function is distributed evenly" . We can not consider the limitation of this problem, according to the number of use of the two methods of discussion:

    • More update, less sumregion
      This situation is relatively simple, we can directly copy the matrix, update the value of the corresponding point directly update, sumregion directly traverse the value within the specified range is possible.

      update: O(1), sumRegion: O(mn)
    • Less update, more sumregion.
      Instead of copying the entire matrix, we can save (0, 0) to the sum of all the elements in the rectangle at each point, so that the complexity of the sumregion is very low, and we need to update all the affected sums when we update.

      update: O(mn), sumRegion: O(1)
    • More update, more sumregion(本题情况)
      We can save a sum for that line, from the starting point to that point, for each point. In this case, if you update, we only need to update the sum of the rows affected. Sumregion, we only need to traverse the corresponding line, plus the corresponding sum of the non-peers.

      update: O(n), sumRegion: O(m)

Of course, for this type of topic, the use of some advanced data structures will be more time-complexity will be lower, can reach Logn, such as two-dimensional line segment tree. This involves only the basic data structure and is as simple as possible.

Complexity of:

Note: M refers to the number of rows, n refers to the number of columns, where global matrix does not count the extra space of each method.

Update

Time:o (n), Space:o (1)

Sumregion

Time:o (M), Space:o (1)

1  Public classNummatrix {2     int[] rowsums;3     4      PublicNummatrix (int[] matrix) {5         if(Matrix.length = =0)6             return;7Rowsums =New int[Matrix.length] [matrix[0].length];8         9         //Build Rowsums MatrixTen          for(inti =0; i < matrix.length; i++) { One              for(intj =0; J < matrix[0].length; J + +) { AROWSUMS[I][J] = Matrix[i][j] + (j = =0?0: Rowsums[i][j-1]); -             } -         } the     } -  -      Public voidUpdateintRowintColintval) { -         //find the difference between the new value and the old value +         intdiff = Val-(Rowsums[row][col]-(col = =0?0: Rowsums[row][col-1])); -          +         //update the row for the affected sum A          for(intj = col; J < rowsums[0].length; J + +) { atROWSUMS[ROW][J] + =diff; -         } -     } -  -      Public intSumregion (intRow1,intCol1,intRow2,intcol2) { -         intres =0; in  -         //sum lines by line, corresponding sum of each row and two sums subtracted to          for(inti = Row1; I <= row2; i++) { +Res + = Rowsums[i][col2]-(col1 = =0?0: Rowsums[i][col1-1]); -         } the         returnRes; *     } $ }Panax Notoginseng  -  the //Your Nummatrix object would be instantiated and called as such: + //Nummatrix Nummatrix = new Nummatrix (matrix); A //nummatrix.sumregion (0, 1, 2, 3); the //nummatrix.update (1, 1, ten); + //nummatrix.sumregion (1, 2, 3, 4);

Range Sum Query 2d-mutable & Immutable

There is no update operation, so we directly find 0, 0 to each point and can.

1  Public classNummatrix {2     int[] matrix;3 4      PublicNummatrix (int[] m) {5Matrix =m;6         if(M = =NULL|| M.length = =0|| m[0].length = =0)return;7          for(inti =1; I < matrix[0].length; i++) {8matrix[0][i] + = matrix[0][i-1];9         }Ten          One          for(inti =1; i < matrix.length; i++) { Amatrix[i][0] + = Matrix[i-1][0]; -         } -          the          for(inti =1; i < matrix.length; i++) { -              for(intj =1; J < matrix[0].length; J + +) { -MATRIX[I][J] + = matrix[i-1][J] + matrix[i][j-1]-Matrix[i-1][j-1]; -             } +         } -     } +  A      Public intSumregion (intRow1,intCol1,intRow2,intcol2) { at         if(Matrix = =NULL)return 0; -          -         if(Row1 = =0&& col1 = =0)returnMatrix[row2][col2]; -         if(Row1 = =0)returnMATRIX[ROW2][COL2]-Matrix[row2][col1-1]; -         if(col1 = =0)returnMATRIX[ROW2][COL2]-Matrix[row1-1][col2]; -          in         returnMATRIX[ROW2][COL2]-Matrix[row1-1][COL2]-Matrix[row2][col1-1] + Matrix[row1-1][col1-1]; -     } to } +  -  the //Your Nummatrix object would be instantiated and called as such: * //Nummatrix Nummatrix = new Nummatrix (matrix); $ //nummatrix.sumregion (0, 1, 2, 3);Panax Notoginseng //nummatrix.sumregion (1, 2, 3, 4);

Range Sum Query 2d-mutable & Immutable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.