Skip to content

Commit d99a27c

Browse files
author
Hao Chen
committed
New Problem "Range Sum Query 2D - Immutable"
1 parent 19a4e23 commit d99a27c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|304|[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | [C++](./algorithms/cpp/rangeSumQuery2D-Immutable/RangeSumQuery2dImmutable.cpp)|Medium|
1112
|303|[Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | [C++](./algorithms/cpp/rangeSumQuery-Immutable/rangeSumQuery-Immutable.cpp)|Easy|
1213
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/) | [C++](./algorithms/cpp/removeInvalidParentheses/RemoveInvalidParentheses.cpp) |Hard|
1314
|300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [C++](./algorithms/cpp/longestIncreasingSubsequence/longestIncreasingSubsequence.cpp)|Medium|
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Source : https://leetcode.com/problems/range-sum-query-2d-immutable/
2+
// Author : Hao Chen
3+
// Date : 2015-11-14
4+
5+
/***************************************************************************************
6+
*
7+
* Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined
8+
* by its upper left corner (row1, col1) and lower right corner (row2, col2).
9+
*
10+
* The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and
11+
* (row2, col2) = (4, 3), which contains sum = 8.
12+
*
13+
* Example:
14+
*
15+
* Given matrix = [
16+
* [3, 0, 1, 4, 2],
17+
* [5, 6, 3, 2, 1],
18+
* [1, 2, 0, 1, 5],
19+
* [4, 1, 0, 1, 7],
20+
* [1, 0, 3, 0, 5]
21+
* ]
22+
*
23+
* sumRegion(2, 1, 4, 3) -> 8
24+
* sumRegion(1, 1, 2, 2) -> 11
25+
* sumRegion(1, 2, 2, 4) -> 12
26+
*
27+
* Note:
28+
*
29+
* You may assume that the matrix does not change.
30+
* There are many calls to sumRegion function.
31+
* You may assume that row1 ≤ row2 and col1 ≤ col2.
32+
*
33+
***************************************************************************************/
34+
35+
36+
/*
37+
*
38+
* Construct a 2D array `sums[row+1][col+1]`
39+
*
40+
* (**notice**: we add additional blank row `sums[0][col+1]={0}` and blank column `sums[row+1][0]={0}`
41+
* to remove the edge case checking), so, we can have the following definition
42+
*
43+
* `sums[i+1][j+1]` represents the sum of area from `matrix[0][0]` to `matrix[i][j]`
44+
*
45+
* To calculate sums, the ideas as below
46+
*
47+
* +-----+-+-------+ +--------+-----+ +-----+---------+ +-----+--------+
48+
* | | | | | | | | | | | | |
49+
* | | | | | | | | | | | | |
50+
* +-----+-+ | +--------+ | | | | +-----+ |
51+
* | | | | = | | + | | | - | |
52+
* +-----+-+ | | | +-----+ | | |
53+
* | | | | | | | |
54+
* | | | | | | | |
55+
* +---------------+ +--------------+ +---------------+ +--------------+
56+
*
57+
* sums[i][j] = sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1] +
58+
*
59+
* matrix[i-1][j-1]
60+
*
61+
* So, we use the same idea to find the specific area's sum.
62+
*
63+
*
64+
*
65+
* +---------------+ +--------------+ +---------------+ +--------------+ +--------------+
66+
* | | | | | | | | | | | | | |
67+
* | (r1,c1) | | | | | | | | | | | | |
68+
* | +------+ | | | | | | | +---------+ | +---+ |
69+
* | | | | = | | | - | | | - | (r1,c2) | + | (r1,c1) |
70+
* | | | | | | | | | | | | | |
71+
* | +------+ | +---------+ | +---+ | | | | |
72+
* | (r2,c2)| | (r2,c2)| | (r2,c1) | | | | |
73+
* +---------------+ +--------------+ +---------------+ +--------------+ +--------------+
74+
*
75+
*
76+
*/
77+
78+
class NumMatrix {
79+
private:
80+
int row, col;
81+
vector<vector<int>> sums;
82+
public:
83+
NumMatrix(vector<vector<int>> &matrix) {
84+
row = matrix.size();
85+
col = row>0 ? matrix[0].size() : 0;
86+
sums = vector<vector<int>>(row+1, vector<int>(col+1, 0));
87+
for(int i=1; i<=row; i++) {
88+
for(int j=1; j<=col; j++) {
89+
sums[i][j] = sums[i-1][j] + sums[i][j-1] - sums[i-1][j-1] + matrix[i-1][j-1];
90+
}
91+
}
92+
}
93+
94+
int sumRegion(int row1, int col1, int row2, int col2) {
95+
return sums[row2+1][col2+1] - sums[row2+1][col1] - sums[row1][col2+1] + sums[row1][col1];
96+
}
97+
};
98+
99+
100+
// Your NumMatrix object will be instantiated and called as such:
101+
// NumMatrix numMatrix(matrix);
102+
// numMatrix.sumRegion(0, 1, 2, 3);
103+
// numMatrix.sumRegion(1, 2, 3, 4);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy