Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the 2D Prefix Sum invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Use cumulative matrix regions and inclusion-exclusion to answer rectangular range queries. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider 2D Prefix Sum when the prompt's constraints and required operations match this shape: Use cumulative matrix regions and inclusion-exclusion to answer rectangular range queries.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A 2D prefix cell stores the sum of the rectangle from the matrix origin up to, but not including, its padded row and column boundaries.
Adding a zero row and column makes every original cell map to prefix row plus one and column plus one. Edge rectangles then use the same formula as interior rectangles.
def prefix_grid(matrix):
if not matrix:return [[0]]
rows,cols=len(matrix),len(matrix[0]);prefix=[[0]*(cols+1) for _ in range(rows+1)]
for row in range(rows):
for col in range(cols):prefix[row+1][col+1]=matrix[row][col]+prefix[row][col+1]+prefix[row+1][col]-prefix[row][col]
return prefixImplement prefix_grid(matrix). Return a rows+1 by cols+1 grid where each cell stores the rectangle sum above and left.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
For a half-open query, take the large bottom-right prefix, subtract the strip above and the strip left, then add their doubly subtracted overlap. This answers immutable rectangle sums in O(1).
def rectangle_sums(matrix,queries):
if not matrix:return []
rows,cols=len(matrix),len(matrix[0]);prefix=[[0]*(cols+1) for _ in range(rows+1)]
for r in range(rows):
for c in range(cols):prefix[r+1][c+1]=matrix[r][c]+prefix[r][c+1]+prefix[r+1][c]-prefix[r][c]
return [prefix[b][right]-prefix[t][right]-prefix[b][left]+prefix[t][left] for t,left,b,right in queries]Implement rectangle_sums(matrix,queries). Queries are half-open [top,left,bottom,right]; return each rectangle sum.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Choose full 2D prefixes for many arbitrary rectangle queries at O(rows × cols) storage. Choose per-row prefixes when query height is small or memory pressure matters, trading each query for O(height).
Say: “Each prefix cell owns the origin rectangle. Query inclusion-exclusion subtracts two outside strips and restores their overlap.” Draw the four corners, state half-open boundaries, and count O(RC + Q) total time.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the 2D Prefix Sum invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| 2D Prefix Sum decision loop | O(rc + q) | O(rc + q) | Build a padded origin-prefix table, then apply four-corner inclusion-exclusion for every query. |
O(rc) for the focused Answer Matrix Region Sums implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Answer Matrix Region Sums before optimizing.
Avoid
def matrix_region_sums(matrix, queries):
passUse instead
def matrix_region_sums(matrix, queries):
rows, cols = len(matrix), len(matrix[0])
prefix = [[0] * (cols + 1) for _ in range(rows + 1)]
for row in range(rows):
running = 0
for col in range(cols):
running += matrix[row][col]
prefix[row + 1][col + 1] = running + prefix[row][col + 1]
results = []
for top, left, bottom, right in queries:
results.append(prefix[bottom + 1][right + 1] - prefix[top][right + 1] - prefix[bottom + 1][left] + prefix[top][left])
return resultsWhere you will hit this: Answer Matrix Region Sums(opens in a new tab)
Subtracts both outside bands without restoring their overlap.
Prevent it: Use the public tests and preserve this state: prefix[r][c] summarizes rows before r and columns before c.
Avoid
def matrix_region_sums(matrix, queries):
p=[[0]*(len(matrix[0])+1) for _ in range(len(matrix)+1)]
for r,row in enumerate(matrix):
for c,v in enumerate(row): p[r+1][c+1]=v+p[r][c+1]+p[r+1][c]-p[r][c]
return [p[b+1][d+1]-p[a][d+1]-p[b+1][l] for a,l,b,d in queries]Use instead
def matrix_region_sums(matrix, queries):
rows, cols = len(matrix), len(matrix[0])
prefix = [[0] * (cols + 1) for _ in range(rows + 1)]
for row in range(rows):
running = 0
for col in range(cols):
running += matrix[row][col]
prefix[row + 1][col + 1] = running + prefix[row][col + 1]
results = []
for top, left, bottom, right in queries:
results.append(prefix[bottom + 1][right + 1] - prefix[top][right + 1] - prefix[bottom + 1][left] + prefix[top][left])
return resultsWhere you will hit this: Answer Matrix Region Sums(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(rc + q).
Avoid
def matrix_region_sums(matrix, queries):
p=[[0]*(len(matrix[0])+1) for _ in range(len(matrix)+1)]
for r,row in enumerate(matrix):
for c,v in enumerate(row): p[r+1][c+1]=v+p[r][c+1]+p[r+1][c]-p[r][c]
return [p[b+1][d+1]-p[a][d+1]-p[b+1][l] for a,l,b,d in queries]Use instead
def matrix_region_sums(matrix, queries):
rows, cols = len(matrix), len(matrix[0])
prefix = [[0] * (cols + 1) for _ in range(rows + 1)]
for row in range(rows):
running = 0
for col in range(cols):
running += matrix[row][col]
prefix[row + 1][col + 1] = running + prefix[row][col + 1]
results = []
for top, left, bottom, right in queries:
results.append(prefix[bottom + 1][right + 1] - prefix[top][right + 1] - prefix[bottom + 1][left] + prefix[top][left])
return resultsWhere you will hit this: Binary Subarrays With Sum(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27
leetcode · checked 2026-07-12