aboutsummaryrefslogtreecommitdiff
path: root/num_submatrices_sum_target.c
blob: 06a3fe21f142724110acf35e1d7f0e5f37933612 (plain)
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
// 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;
}