[Coding Made Simple] Sum Query in 2D Immutable Array

Given a 2D immutable array,  Write an efficient program to support any given sub-rectangle sum query in this 2D matrix.

A simple solution is to add each entry inside the sub-rectangle. The runtime is O(n * m).

The problem of this solution is that for a given 2D matrix, we always start from scratch to compute the sum of a sub-rectangle.

Dynamic programming should be used to optimize the runtime.

State: T[i][j]: the sum of sub rectangle with top left corner (0, 0) and bottom right corner (i - 1, j - 1).

Function: T[i][j] = T[i - 1][j] + T[i][j - 1] + matrix[i - 1][j - 1] - T[i - 1][j - 1].

Initialization: T[0][j] = T[i][0] = 0;

Answer:  given top left corner(x1, y1) and bottom right corner(x2, y2),

    Sum of sub rectangle(x1, y1, x2, y2) is T[x2 + 1][y2 + 1] - T[x1][y2 + 1] - T[x2 + 1][y1] + T[x1][y1].

 1 public class SumQuery {
 2     private int[][] T;
 3     public SumQuery(int[][] matrix) {
 4         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
 5             T = null;
 6         }
 7         T = new int[matrix.length + 1][matrix[0].length + 1];
 8         for(int i = 0; i < T.length; i++) {
 9             T[i][0] = 0;
10         }
11         for(int j = 0; j < T[0].length; j++) {
12             T[0][j] = 0;
13         }
14         for(int i = 1; i < T.length; i++) {
15             for(int j = 1; j < T[0].length; j++) {
16                 T[i][j] = T[i - 1][j] + T[i][j - 1] + matrix[i - 1][j - 1] - T[i - 1][j - 1];
17             }
18         }
19     }
20     public int querySubRectangleSum(int x1, int y1, int x2, int y2) {
21         x1++; y1++; x2++; y2++;
22         return T[x2][y2] - T[x1 - 1][y2] - T[x2][y1 - 1] + T[x1 - 1][y1 - 1];
23     }
24 }
时间: 2024-10-06 07:36:20

[Coding Made Simple] Sum Query in 2D Immutable Array的相关文章

2-D range sum query implementation(2-D segment tree)

Google interview question:一个二维数组,有两个方法,一个是update(x,y),更新一个cell的值,一个是query(x1,y1,x2,y2),查询(x1,y1,x2,y2)矩形内所有元素的和. Senario 1. update调用次数远大于query. Senario 2. query调用次数远大于update. Senario 3. 两种方法调用一样多. 对于senario 1,只需要用一个二维数组储存数据,update相应的cell,时间复杂度O(1),qu

[LeetCode] Range Sum Query 2D - Immutable

Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fact, if you work in computer vision, you may know a name for such an array --- Integral Image. To solve this problem, Stefan has already posted a very e

[LeetCode] Range Sum Query - Immutable &amp; Range Sum Query 2D - Immutable

Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You

304. Range Sum Query 2D - Immutable

/* * 304. Range Sum Query 2D - Immutable * 2016-7-3 by Mingyang * 这个题目自己做了一下同样的dp思路,但是out of boundry,为什么呢? * 因为自己老老实实的把dp的维度跟matrix的维度设成一样的了 * 那我们就把dp的维度再多设一个这样就很好地表示了 * dp[i][j]仅仅表示到i-1,j-1这个点(inclusive)的和的大小 */ class NumMatrix { private int[][] df;

304. Range Sum Query 2D - Immutable java solutions

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, co

Range Sum Query 2D - Mutable &amp; Immutable

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,

Range Sum Query 2D - Immutable

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, co

303. Range Sum Query - Immutable

303. Range Sum Query - Immutable Total Accepted: 10632 Total Submissions: 44726 Difficulty: Easy Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRang

[LeetCode] 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, co