1224. Number of Submatrices That Sum to Target

MediumDynamic Programming2D DPPrefix Sum

Given a matrix and an integer target, count the non-empty submatrices whose elements sum exactly to the target. Two submatrices are different if they differ in any cell coordinate. The input is JSON {matrix, target}.

Input: JSON {matrix, target}.

Output: Integer — the number of submatrices summing to target.

Examples

Example 1
Input: {"matrix":[[0,1,0],[1,1,1],[0,1,0]],"target":0}
Output: 4
Explanation: Four submatrices sum to 0.
Example 2
Input: {"matrix":[[1,-1],[-1,1]],"target":0}
Output: 5
Explanation: Five submatrices sum to 0.
Example 3
Input: {"matrix":[[904]],"target":0}
Output: 0
Explanation: No submatrix sums to 0.

Constraints

Asked by

AmazonGoogleMeta
Solve this problem in the editor →