Advertisement

Number of Submatrices That Sum to Target - LeetCode 1074 Solution

Number of Submatrices That Sum to Target - Complete Solution Guide

Number of Submatrices That Sum to Target is LeetCode problem 1074, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: mat

Detailed Explanation

The problem asks us to find the number of non-empty submatrices within a given matrix whose elements sum up to a specified target value. A submatrix is defined by its top-left and bottom-right corners (x1, y1, x2, y2), and consists of all cells matrix[x][y] where x1 <= x <= x2 and y1 <= y <= y2. We need to count how many different submatrices satisfy this sum condition. The constraints limit the matrix dimensions to a maximum of 100x100, and the values within the matrix and the target value have specific ranges as described.

Solution Approach

The solution iterates through all possible submatrices by considering every possible pair of starting and ending rows (r1 and r2). For each pair of rows, it calculates a running sum of the columns between those rows. Then, it uses a hash map to store the frequencies of different prefix sums encountered while iterating through the columns. By checking if `current_sum - target` exists in the hash map, we can determine if there's a submatrix ending at the current column whose sum equals the target.

Step-by-Step Algorithm

  1. Step 1: Iterate through all possible starting rows (r1) from 0 to m-1.
  2. Step 2: For each starting row r1, iterate through all possible ending rows (r2) from r1 to m-1.
  3. Step 3: Initialize an array `col_sums` to store the sum of elements in each column between rows r1 and r2.
  4. Step 4: Iterate through all columns (c) from 0 to n-1 and calculate `col_sums[c]` by adding matrix[r2][c] to it for each row r2 between r1 and r2 inclusive.
  5. Step 5: Initialize a hash map `prefix_sum_counts` to store the frequency of prefix sums. Set `prefix_sum_counts[0] = 1` to account for the case where the submatrix starting from the first column has a sum equal to the target.
  6. Step 6: Initialize `current_sum` to 0.
  7. Step 7: Iterate through each column sum `s` in `col_sums`.
  8. Step 8: Add `s` to `current_sum`.
  9. Step 9: Check if `current_sum - target` exists as a key in `prefix_sum_counts`. If it does, increment the `count` by the value associated with that key (i.e., the frequency of that prefix sum).
  10. Step 10: Increment the frequency of `current_sum` in `prefix_sum_counts`.
  11. Step 11: Return the final `count`.

Key Insights

  • Insight 1: We can iterate through all possible pairs of rows (r1, r2) to define the top and bottom boundaries of our submatrices.
  • Insight 2: For each pair of rows (r1, r2), we can calculate the column sums between these rows.
  • Insight 3: Once we have the column sums, we can use a hash map (prefix sum counting) to efficiently find the number of submatrices with a sum equal to the target within those columns.

Complexity Analysis

Time Complexity: O(m^2*n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Matrix, Prefix Sum.

Companies

Asked at: Media.net.