File tree 1 file changed +5
-4
lines changed
scripts/algorithms/M/Matrix Block Sum
1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 88 ms (Top 26.15%) | Memory: 43.2 MB (Top 92.07%)
1
2
class Solution {
2
3
public int [][] matrixBlockSum (int [][] mat , int k ) {
3
4
int m = mat .length ,n = mat [0 ].length ;
4
5
int [][] answer = new int [m ][n ];
5
6
for (int i =0 ;i <m ;i ++){
6
7
for (int j =0 ;j <n ;j ++){
7
8
int val = 0 ;
8
- // take new variables to get starting index of mat[r][c]
9
+ // take new variables to get starting index of mat[r][c]
9
10
int p = i -k ,q =j -k ;
10
- //make sure p and q are atleast 0 (i.e. valid)
11
+ //make sure p and q are atleast 0 (i.e. valid)
11
12
while (p <0 )
12
13
p ++;
13
14
while (q <0 )
14
15
q ++;
15
- //traverse again in the matrix with starting at p,q and ending at i+k and j+k
16
- //add conditions to make sure the indices dont cross the values of m and n
16
+ //traverse again in the matrix with starting at p,q and ending at i+k and j+k
17
+ //add conditions to make sure the indices dont cross the values of m and n
17
18
for (int x = p ;x <=i +k && x <m ;x ++){
18
19
for (int y = q ;y <=j +k && y <n ;y ++){
19
20
val += mat [x ][y ];
You can’t perform that action at this time.
0 commit comments