bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1074. Number of Submatrices That Sum to Target

1074. Number of Submatrices That Sum to Target

Leetcode Solutions

Number of Subarrays that Sum to Target: HorizontalD Prefix Sum

  1. Initialize the result count to 0.
  2. Compute the number of rows r and columns c.
  3. Compute the 2D prefix sum ps.
  4. Iterate over the rows with two pointers r1 and r2.
  5. For each pair of rows, initialize a hashmap to store the frequency of 1D prefix sums.
  6. Iterate over the columns, updating the current 1D prefix sum using the 2D prefix sum.
  7. For each column, update count by adding the frequency of curr_sum - target from the hashmap.
  8. Update the hashmap with the current 1D prefix sum.
  9. Return count.
UML Thumbnail

Number of Subarrays that Sum to Target: VerticalD Prefix Sum

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...