-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0304.cpp
More file actions
executable file
·41 lines (36 loc) · 1.13 KB
/
LC0304.cpp
File metadata and controls
executable file
·41 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Problem Statement: https://leetcode.com/problems/range-sum-query-2d-immutable/
Space: O(m • n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
|-----------------------------------|----------|-------|
| Operations | Time | Space |
|-----------------------------------|----------|-------|
| NumMatrix(matrix) | O(m • n) | O(1) |
| sumRegion(row1, col1, row2, col2) | O(1) | O(1) |
|-----------------------------------|----------|-------|
*/
class NumMatrix {
private:
vector<vector<int>> prefix;
public:
NumMatrix(vector<vector<int>>& matrix) {
// base case
if (matrix.empty())
return;
int m = matrix.size(), n = matrix[0].size();
prefix = vector<vector<int>>(m + 1, vector<int>(n + 1));
// 2-dimensional prefix sum
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
prefix[i + 1][j + 1] = matrix[i][j]
+ prefix[i + 1][j]
+ prefix[i][j + 1]
- prefix[i][j];
}
int sumRegion(int row1, int col1, int row2, int col2) {
return prefix[row2 + 1][col2 + 1]
- prefix[row2 + 1][col1]
- prefix[row1][col2 + 1]
+ prefix[row1][col1];
}
};