Number of Submatrices That Sum to Target
Updated
Problem statement
Given an m x n integer matrix matrix and an integer target, return how many non-empty submatrices have values that add up to exactly target.
A submatrix is a rectangle of cells, chosen by a top row r1, a bottom row r2, a left column c1 and a right column c2 with r1 <= r2 and c1 <= c2. It holds every cell matrix[r][c] with r1 <= r <= r2 and c1 <= c <= c2. Two submatrices are different when at least one of these four bounds differs, even if they hold the same values.
Values can be negative, zero or positive.
Function
numSubmatrixSumTarget(matrix: int[][], target: int) → intExamples
Example 1
matrix = [[2,-1],[1,0]]target = 1return = 3Three submatrices add up to 1: the single cell 1 at row 1, column 0; the whole top row [2, -1]; and the whole bottom row [1, 0].
Example 2
matrix = [[0,0],[0,0]]target = 0return = 9Every one of the 9 submatrices adds up to 0: 4 single cells, 2 full rows, 2 full columns and the whole matrix. Equal values in different places still count as different submatrices.
Example 3
matrix = [[4,-2,3],[-1,5,-4]]target = 3return = 3Three submatrices add up to 3: the single cell 3 in the top row; the first column, 4 + (-1); and the second column, -2 + 5. The whole matrix adds up to 5, so it does not count.
Example 4
matrix = [[5]]target = 5return = 1The only submatrix is the single cell, and it equals the target.
Example 5
matrix = [[1,2,3]]target = 7return = 0The row adds up to at most 6, so no submatrix reaches 7.
Constraints
m == matrix.lengthn == matrix[i].length1 <= m, n <= 100-1000 <= matrix[i][j] <= 1000-108 <= target <= 108
More WhatNot Onsite Interview questions
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Detect Blocked Chat PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Minimum Remove to Make Valid ParenthesesONSITE INTERVIEW · Seen Dec 2024
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2024
- Merge Two Sorted ListsONSITE INTERVIEW · Seen Jul 2024
- Parse and Store Log RecordsONSITE INTERVIEW · Seen Oct 2023
- Room Occupancy at Query TimesONSITE INTERVIEW · Seen Oct 2023
- K Smallest Values in a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Average Purchase PriceONSITE INTERVIEW · Seen Sep 2022
Similar questions at other companies
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
- Count Subarrays with Equal Even and Odd CountsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Validate Rectangular Game PiecesGoogle · ONSITE INTERVIEWGoogle interview questions
- Count Subarrays with Sum KMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions