0
Leetcode Problem 1074. Number of Submatrices That Sum to Target
1074. Number of Submatrices That Sum to Target
AI Mock Interview
Leetcode Solutions
Number of Subarrays that Sum to Target: HorizontalD Prefix Sum
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize the result
count
to 0.
Compute the number of rows
r
and columns
c
.
Compute the 2D prefix sum
ps
.
Iterate over the rows with two pointers
r1
and
r2
.
For each pair of rows, initialize a hashmap to store the frequency of 1D prefix sums.
Iterate over the columns, updating the current 1D prefix sum using the 2D prefix sum.
For each column, update
count
by adding the frequency of
curr_sum - target
from the hashmap.
Update the hashmap with the current 1D prefix sum.
Return
count
.
Number of Subarrays that Sum to Target: VerticalD Prefix Sum
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...