Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number.
Copy [
[1 ,5 ,7],
[3 ,7 ,-8],
[4 ,-8 ,9],
]
O(n3) time.
Copy public int [][] submatrixSum( int [][] matrix) {
if (matrix == null || matrix . length == 0 || matrix[ 0 ] . length == 0 ) {
return null ;
}
int n = matrix . length ;
int m = matrix[ 0 ] . length ;
int [][] res = new int [ 2 ][ 2 ];
// calculate matrix prefix sum
int [][] sum = new int [n + 1 ][m + 1 ];
// sum[i][j] - sum from m[0,0] to m[i,j]
for ( int i = 1 ; i <= n; i ++ ) {
for ( int j = 1 ; j <= m; j ++ ) {
sum[i][j] = matrix[i - 1 ][j - 1 ] + sum[i - 1 ][j] + sum[i][j - 1 ] - sum[i - 1 ][j - 1 ];
}
}
// try to fix height then loop through width
// then use subarray sum I
for ( int l = 0 ; l < n; l ++ ) {
for ( int h = l + 1 ; h <= n; h ++ ) {
HashMap < Integer , Integer > hm = new HashMap <>();
for ( int j = 0 ; j <= m; j ++ ) {
int diff = sum[h][j] - sum[l][j];
if ( hm . containsKey (diff)) {
res[ 0 ][ 0 ] = l;
res[ 0 ][ 1 ] = hm . get (diff);
res[ 1 ][ 0 ] = h - 1 ;
res[ 1 ][ 1 ] = j - 1 ;
return res;
} else {
hm . put (diff , j);
}
}
}
}
return res;
}