diff options
Diffstat (limited to 'num_submatrices_sum_target.c')
-rw-r--r-- | num_submatrices_sum_target.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/num_submatrices_sum_target.c b/num_submatrices_sum_target.c new file mode 100644 index 0000000..06a3fe2 --- /dev/null +++ b/num_submatrices_sum_target.c @@ -0,0 +1,31 @@ +// 1074. Number of Submatrices That Sum to Target +#include "leetcode.h" + +/* + * given a 'matrix' and 'target', return the number of non-empty submatrices + * that sum to target. a submatrix 'x1, y1 x2, y2' is the set of all cells + * 'matrix[x][y]' with 'x1 <= x <= x2' and 'y1 <= y <= y2'. two submatrices are + * different if they have some coordinate that is different. for example if x1 + * != x1 + */ + +int numSubmatrixSumTarget(int **matrix, int matrix_size, int *matrix_col_size, + int target) { + int r_size = matrix_col_size[0], dp[r_size], ans = 0, y = 0; + while (y < matrix_size) { + memset(dp, 0, sizeof(int) * r_size); + for (int row = y; row < matrix_size; row++) + for (int col = 0; col < r_size; col++) + dp[col] += matrix[row][col]; + for (int col = 0; col < r_size; col++) { + int sum = 0; + for (int _col = col; _col < r_size; _col++) { + sum += dp[_col]; + if (sum == target) + ans++; + } + } + y++; + } + return ans; +} |