Given a matrix and a 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 (x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.
Example 1:
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0 Output: 4 Explanation: The four 1x1 submatrices that only contain 0.
Example 2:
Input: matrix = [[1,-1],[-1,1]], target = 0 Output: 5 Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
Example 3:
Input: matrix = [[904]], target = 0 Output: 0
Constraints:
1 <= matrix.length <= 1001 <= matrix[0].length <= 100-1000 <= matrix[i][j] <= 1000-10^8 <= target <= 10^8When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find all rectangular blocks of numbers within a bigger grid that add up to a specific target value. The brute force approach tries every possible rectangular block, no matter how big or small, and checks if its numbers sum up to the target.
Here's how the algorithm would work step-by-step:
def num_submatrices_that_sum_to_target(matrix, target):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
count = 0
for top_row in range(number_of_rows):
for left_column in range(number_of_columns):
# Iterate to define the submatrix's size
for bottom_row in range(top_row, number_of_rows):
for right_column in range(left_column, number_of_columns):
submatrix_sum = 0
# Calculate sum of elements in the submatrix
for row_index in range(top_row, bottom_row + 1):
for column_index in range(left_column, right_column + 1):
submatrix_sum += matrix[row_index][column_index]
if submatrix_sum == target:
count += 1
return countThe core idea is to calculate sums of rectangles very quickly. We achieve this by pre-calculating the sum of all rectangles starting from the top-left corner. This pre-calculation then lets us find the sum of *any* rectangle using only a few simple operations.
Here's how the algorithm would work step-by-step:
def num_submatrix_sum_target(matrix, target):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
# Create a prefix sum matrix.
prefix_sum_matrix = [[0] * (number_of_columns + 1) for _ in range(number_of_rows + 1)]
for row_index in range(1, number_of_rows + 1):
for column_index in range(1, number_of_columns + 1):
prefix_sum_matrix[row_index][column_index] = prefix_sum_matrix[row_index - 1][column_index] +\
prefix_sum_matrix[row_index][column_index - 1] -\
prefix_sum_matrix[row_index - 1][column_index - 1] +\
matrix[row_index-1][column_index-1]
count = 0
# Iterate through all possible top and bottom rows.
for top_row_index in range(1, number_of_rows + 1):
for bottom_row_index in range(top_row_index, number_of_rows + 1):
# Calculate submatrix sums and count.
for left_column_index in range(1, number_of_columns + 1):
for right_column_index in range(left_column_index, number_of_columns + 1):
# Use prefix sums to calculate submatrix sum.
submatrix_sum = prefix_sum_matrix[bottom_row_index][right_column_index] -\
prefix_sum_matrix[top_row_index - 1][right_column_index] -\
prefix_sum_matrix[bottom_row_index][left_column_index - 1] +\
prefix_sum_matrix[top_row_index - 1][left_column_index - 1]
# Increment count if submatrix sum equals target.
if submatrix_sum == target:
count += 1
return count| Case | How to Handle |
|---|---|
| Null or empty matrix | Return 0 immediately as there are no submatrices to consider. |
| Matrix with only one row or one column | The algorithm should still function correctly, effectively reducing to a 1D prefix sum problem. |
| Large matrix dimensions (potential for memory issues) | The solution must be efficient in space and time complexity to avoid memory limits exceeded errors. |
| Matrix containing all zeros | The code must correctly count submatrices that sum to zero if the target is also zero. |
| Matrix with all negative numbers and a positive target | The code must return 0 since no submatrix can sum to a positive value. |
| Target value is extremely large, potentially causing integer overflow during sums | Use appropriate data types (long) to prevent potential integer overflows during submatrix sum calculations. |
| Target is zero and matrix contains both positive and negative numbers. | The solution should accurately count submatrices summing to zero considering both positive and negative values. |
| No submatrix sums to the target value | The algorithm should correctly return 0 when no matching submatrix is found. |