-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMatrixBlockSum.java
50 lines (42 loc) · 1.43 KB
/
MatrixBlockSum.java
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
42
43
44
45
46
47
48
49
50
/*https://leetcode.com/problems/matrix-block-sum/*/
/*simple solution*/
class Solution {
public int[][] matrixBlockSum(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
int[][] result = new int[m][n];
int i, j, p, q;
int elem;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
elem = mat[i][j];
for (p = Math.max(0,i-k); p <= Math.min(m-1,i+k); ++p)
for (q = Math.max(0,j-k); q <= Math.min(n-1,j+k); ++q)
result[p][q] += elem;
}
}
return result;
}
}
/*prefix sum solution*/
class Solution {
public int[][] matrixBlockSum(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
int[][] result = new int[m][n];
int i, j, p, q, r, c;
int elem;
for (i = 0; i < m; ++i) result[i][0] = mat[i][0];
for (i = 0; i < m; ++i) for (j = 1; j < n; ++j) result[i][j] = result[i][j-1]+mat[i][j];
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
mat[i][j] = 0;
r = Math.min(m-1,i+k);
c = Math.min(n-1,j+k);
for (p = Math.max(i-k,0); p <= r; ++p)
mat[i][j] += result[p][c]-(j-(k+1) >= 0 ? result[p][j-(k+1)] : 0);
}
return mat;
}
}